user35131
user35131

Reputation: 1134

How to do a group by and count by condition in R

df2<- df %>%
group_by(ID)%>%
summarise(Count= sum(df$value=="Y"))

However when I do this I get Count column as NA

df2<- df %>%
group_by(ID)%>%
summarise(Count= sum(value=="Y"))

Same when I do this. I would like to get the numbers to show up.

Upvotes: 0

Views: 54

Answers (1)

Nir Graham
Nir Graham

Reputation: 5167

if value is ever NA it will spoil your sum so

df2<- df %>%
  group_by(ID)%>%
  summarise(Count= sum(value=="Y",na.rm=TRUE))

Upvotes: 1

Related Questions