Suppose I have the following data:
data = data.frame(
name=c("bob", "bob", "mary", "mary", "mary"),
colour=c("blue", "blue", "blue", "green", "green"),
number=c(1,1,1,2,3))
data
name colour number
1 bob blue 1
2 bob blue 1
3 mary blue 1
4 mary green 2
5 mary green 3
How can I concatenate the above on two columns, removing any repeated strings? I have tried:
data <- data %>% group_by(`name`) %>%
summarise_all(funs(paste(na.omit(.), collapse = ", ")))
But get the following which is incorrect:
name colour number
1 bob blue, blue 1, 1
2 mary blue, green, green 1, 2, 3
Expected output:
name colour number
1 bob blue 1
2 mary blue, green 1,2,3