Reputation: 41
I have a data frame with many columns. I need to get a list for arbitrary columns and the number of rows that meet some conditions based on those columns. For example, data frame has column a, b and c, I need to get row counts that b > 0 for b, and c > 0 for c in the form of
column count
b 23
c 12
What is the easiest way?
Upvotes: 0
Views: 65
Reputation: 70336
You could use lapply
so that you don't need to repeat typing the same function for each column you want:
lapply(df[,c("b","c")], function(x) sum(x > 0))
In this case, you specify which columns you want the output for in the df[,c("b","c")]
or you could replace that with just df
to check all columns.
The output would be a list, which may be easier to handle than many separate values.
Upvotes: 2