Reputation: 119
In the following code, the 2rd payment for item b is zero value. Using the pipe %>%
, is possible to show the count for item b as 2 not 3 since there is no payment for b at the 2rd payment?
df <-data.frame("item" = c("a","b", "b","a","b"), "payment" = c(10,20,0,40,30) )
df_sum <-
df %>%
group_by(item) %>%
summarise(total = sum(payment),
totalcount =n())
Upvotes: 1
Views: 1439
Reputation: 1648
You can filter out rows you don't want. E.g., if you don't want to count rows where payment = 0
, you can use filter
:
df %>%
group_by(item) %>%
filter(payment > 0) %>%
summarise(total = sum(payment),
totalcount =n())
# A tibble: 2 x 3
item total totalcount
<fct> <dbl> <int>
1 a 50.0 2
2 b 50.0 2
Upvotes: 2