How to count TRUE values in a logical vector

后端 未结 8 1608
[愿得一人]
[愿得一人] 2020-11-28 01:17

In R, what is the most efficient/idiomatic way to count the number of TRUE values in a logical vector? I can think of two ways:

z <- sample(c         


        
8条回答
  •  长情又很酷
    2020-11-28 02:18

    Another option which hasn't been mentioned is to use which:

    length(which(z))
    

    Just to actually provide some context on the "which is faster question", it's always easiest just to test yourself. I made the vector much larger for comparison:

    z <- sample(c(TRUE,FALSE),1000000,rep=TRUE)
    system.time(sum(z))
       user  system elapsed 
       0.03    0.00    0.03
    system.time(length(z[z==TRUE]))
       user  system elapsed 
       0.75    0.07    0.83 
    system.time(length(which(z)))
       user  system elapsed 
       1.34    0.28    1.64 
    system.time(table(z)["TRUE"])
       user  system elapsed 
      10.62    0.52   11.19 
    

    So clearly using sum is the best approach in this case. You may also want to check for NA values as Marek suggested.

    Just to add a note regarding NA values and the which function:

    > which(c(T, F, NA, NULL, T, F))
    [1] 1 4
    > which(!c(T, F, NA, NULL, T, F))
    [1] 2 5
    

    Note that which only checks for logical TRUE, so it essentially ignores non-logical values.

提交回复
热议问题