I have a list which looks like this:
> str(my_list)
List of 20000
$ : num 1
$ : num 1
$ : num 0.667
$ : num 1
$ : num 1
$ : num 1
$ : num 1
$ : num 0.667
$ : num 1
$ : num 1
$ : num 1
$ : num 1
$ : num 1
$ : num 1
. . .
And I want to create an lapply that will tell me how many of the elements of my list are < 1, which is a logical condition of course. To me, this sounds like something I could probably do rather easily just using a COUNTIF function in Excel.
CodePudding user response:
If it is a list
having each element of length 1, just unlist
it to a vector and get the sum
of logical vector (< 1
) will give the count of the number of list elements with less than 1
sum(unlist(my_list) < 1, na.rm = TRUE)
If the list element have length > 1, we may have to either loop over the list with sapply
, create a logical vector, and maybe wrap with any
or all
(if the intention is to find any element less than 1 or all elements)
sum(sapply(my_list, \(x) any(x < 1, na.rm = TRUE)))