Count the number of non-zero elements of each column

后端 未结 3 1854
时光说笑
时光说笑 2020-12-05 06:42

Very new to R and I have a .rda file that contains a matrix of gene IDs and counts for each ID in 96 columns. It looks like this:

3条回答
  •  一个人的身影
    2020-12-05 07:28

    Another method using plyr's numcolwise:

    library(plyr)
    
    dat <- data.frame(a = sample(1:25, 25),
                      b = rep(0, 25),
                      c = sample(1:25, 25))
    nonzero <- function(x) sum(x != 0)
    numcolwise(nonzero)(dat)
       a b  c
    1 25 0 25
    

提交回复
热议问题