Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
561 views
in Technique[技术] by (71.8m points)

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

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

We can use

colSums(myDF[c("L2", "L3", "L4")] > 0)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...