aggregate toString ignoring NA values / Concatenate rows including NAs
Solution 1:
df %>%
group_by(id, year) %>%
summarise(across(everything(), ~toString(na.omit(.x))))
# A tibble: 3 x 4
# Groups: id [3]
id year cat_1 cat_2
<int> <int> <chr> <chr>
1 1 2021 Too high, YOY error "YOY error"
2 2 2021 Too high "Too low, YOY error"
3 3 2021 Too high, YOY error ""
Base R:
aggregate(.~id + year, df, \(x)toString(na.omit(x)), na.action = identity)
id year cat_1 cat_2
1 1 2021 Too high, YOY error YOY error
2 2 2021 Too high Too low, YOY error
3 3 2021 Too high, YOY error