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 subset function, I'm just not sure how Thanks!
L1 L2 L3 L4
1 1 0 2
2 1 4 1
3 1 3 1
2 2 1 1
I would like to be able to create a function that would be able to tally up the number of rows in columns L2, L3, and L4 greater than 0.
We can use
colSums(myDF[c("L2", "L3", "L4")] > 0)
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))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With