How to count TRUE values in a logical vector

后端 未结 8 1575
[愿得一人]
[愿得一人] 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:02

    I've just had a particular problem where I had to count the number of true statements from a logical vector and this worked best for me...

    length(grep(TRUE, (gene.rep.matrix[i,1:6] > 1))) > 5
    

    So This takes a subset of the gene.rep.matrix object, and applies a logical test, returning a logical vector. This vector is put as an argument to grep, which returns the locations of any TRUE entries. Length then calculates how many entries grep finds, thus giving the number of TRUE entries.

提交回复
热议问题