I have a large data frame (1616610 rows, 255 columns) and I need to paste together the unique values of each column based on a key.
For example:
>
You could do the following with dplyr
func_paste <- function(x) paste(unique(x), collapse = ', ')
data %>%
group_by(a) %>%
summarise_each(funs(func_paste))
## a b c d
## (dbl) (chr) (chr) (chr)
##1 1 apples, oranges 12, 22 Monday
##2 2 apples 45, 67 Tuesday, Wednesday
##3 3 grapefruit 28 Tuesday
Moved from comments:
library(data.table)
dt <- as.data.table(data)
dt[, lapply(.SD, function(x) toString(unique(x))), by = a]
giving:
a b c d
1: 1 apples, oranges 12, 22 Monday
2: 2 apples 45, 67 Tuesday, Wednesday
3: 3 grapefruit 28 Tuesday