Counting with condition in summarize and group_by in R

Viewed 41

I have following df

item cash
A 1000
B 2800
A 2000
B 1500
A 3000
A 4000

My desired output is,

item count1 count2
A 2 1
B 1 0

What I have tried so far is,

df_summary <- df %>%
    group_by(item) %>%
    summarise(count1 = sum(cash > 2000),
              count2 = sum(cash <= 1000)) 

But it gives NA for the count1 and count2 columns. I checked cash column in numeric. Can anyone please help?

0 Answers
Related