0

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?

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Good to hear that. So if your question is answered, you can accept the answer that worked by clicking the check mark on the left indicating to others that this question is done.
0
sum(df$b > 0)

sum(df$c > 0)

1 Comment

Please add a bit of explanation. Code only is (sometimes) good, but code AND explanation is (most of times) better.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.