I have a data frame and I am trying to collapse a bunch of columns by a group column called "id", but I get a lot of NA values. Is there a way to remove NA, or use only unique values in the paste function?
df<- data.frame(id=c(1, 1, 2, 3, 3, 3),
bar=c('a', 'b', 'a', 'b', 'c', 'NA'),
foo=c('NA', 'b', 'a', 'b', 'NA', 'NA'))
library(plyr)
df %>%
group_by(id) %>%
summarise_all(funs(unique(paste(., collapse = ","))))
I would like to remove the "NA"... Also, this functions takes longer than the basic aggregate, however I have not found the correct formula for the aggregate function either:
aggregate(df, by=list(df$id), paste, collapse=",")
Thanks for your help!