counting the number of values greater than 0 in R in multiple columns

前端 未结 2 682
旧时难觅i
旧时难觅i 2021-01-26 07:29

I have a dataset myDF in R with a the variables L1,L2,L3,L4. How can I get the number of observations in L2, L3, and L4 that area greater than 0?

I would like to use the

相关标签:
2条回答
  • 2021-01-26 07:55

    We can use

    colSums(myDF[c("L2", "L3", "L4")] > 0)
    
    0 讨论(0)
  • 2021-01-26 08:16

    I don't think colSums will give you the right answer since it doesn't counts the number of observations, but only sums the columns' values.

    I think that this will give you what you want , I hope.

    apply(myDF,2,function(x) sum(x > 0))
    
    0 讨论(0)
提交回复
热议问题