Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Summarize with conditions based on ranges in dplyr

Tags:

r

dplyr

summarize

There is an illustration of my example. Sample data:

 df <- data.frame(ID = c(1, 1, 2, 2, 3, 5), A = c("foo", "bar", "foo", "foo", "bar", "bar"),
 B =     c(1, 5, 7, 23, 54, 202))

df
  ID   A   B
1  1 foo   1
2  1 bar   5
3  2 foo   7
4  2 foo  23
5  3 bar  54
6  5 bar 202

What I want to do is to summarize, by ID, and count of the same IDs. Furthermore, I want frequencies of IDs in subgroups based values of B in different numeric ranges (number of observations with B>=0 & B<5, B>=5 & B<10, B>=10 & B<15, B>=15 & B<20 etc for all IDs).

I want this result:

  ID count count_0_5 count_5_10 etc
1  1    2          1          1 etc
2  2    2         NA          1 etc
3  3    1         NA         NA etc
4  5    1         NA         NA etc

I tried this code using package dplyr:

df %>%
  group_by(ID) %>%
  summarize(count=n(), count_0_5 = n(B>=0 & B<5))

However, it returns this error:

`Error in n(B>=0 & B<5) : 
  unused argument (B>=0 & B<5)`
like image 309
Vojtěch Kania Avatar asked Oct 24 '25 07:10

Vojtěch Kania


1 Answers

Perhaps replacing n(B>=0 & B<5) with sum(B>=0 & B<5)?

This will sum the number of cases where the two specified conditions are accomplished.

However, you'll get 0's instead of NA's. This can be settled by: ifelse(sum(B>=0 & B<5)>0, sum(B>=0 & B<5), NA)

I'm pretty sure that there may be a better solution (more clearer and efficient), but this should work!

like image 150
Dave Avatar answered Oct 25 '25 22:10

Dave



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!