For these purposes, there are the summarise_all
, summarise_at
, and summarise_if
functions. Using summarise_all
:
df %>%
group_by(Sample) %>%
summarise_all(funs(paste(na.omit(.), collapse = ",")))
# A tibble: 3 × 5
Sample group Gene1 Gene2 Gene3
1 A 1,2 a,b
2 B 1 c
3 C 1,2,3 a,b,c d,e