I want to use dplyr summarise to sum counts by groups. Specifically I want to remove NA values if not all summed values are NA, but if all summed values are NA, I want to display NA. For example:
name <- c("jack", "jack", "mary", "mary", "ellen", "ellen")
number <- c(1,2,1,NA,NA,NA)
df <- data.frame(name,number)
In this case I want the following result:
- Jack = 3
- Mary = 1
- Ellen = NA
However if I set na.rm = F
:
df %>% group_by(name) %>% summarise(number = sum(number, na.rm = F))
The result is:
- Jack = 3
- Mary = NA
- Ellen = NA
And if i set na.rm = T
:
df %>% group_by(name) %>% summarise(number = sum(number, na.rm = T))
The result is
- Jack = 3
- Mary = 1
- Ellen = 0
How can I solve this so that the cases with numbers and NA's get a number as output, but the cases with only NA's get NA as output.