I want to group by ID and summarise, whilst removing NAs.
please see example code below.
# Example data
ID <- c(1, 1, 1, 2, 2, 3, 3)
x <- c(2, 3, NA, 2, 3, 1, 1)
ID_x <- tibble(ID, x)
# 1. Works
ID_x %>%
group_by(ID) %>%
summarise_each(mean)
# 2. Does not work with na.rm=TRUE
ID_x %>%
group_by(ID) %>%
summarise_each(mean(., na.rm=TRUE))
Thanks in advance
>Solution :
Use the lambda (~
library(dplyr)
ID_x %>%
group_by(ID) %>%
summarise_each(~ mean(., na.rm=TRUE))
-output
# A tibble: 3 × 2
ID x
<dbl> <dbl>
1 1 2.5
2 2 2.5
3 3 1
Also, in recent versions, the summarise_each will accompany a warning as these are deprecated in favor of across
ID_x %>%
group_by(ID) %>%
summarise(across(everything(), ~ mean(., na.rm=TRUE)))