This is going to be pretty and cryptic looking:
aggregate(c ~ a + b, x, I)
# a b c
# 1 1 11 101, 103, 105, 107
# 2 1 12 102, 104, 106
# 3 2 12 108
# 4 2 16 109
# 5 2 17 110
The I
function (you can also use c
) would create a list
in your third column. You don't need to create a separate data.frame
for the unique combinations of "a" and "b". Just use them as the grouping variables in aggregate
.
Of course, there are many other ways to do this.
Here's data.table
:
library(data.table)
X <- as.data.table(x)
X[, list(c = list(I(c))), by = list(a, b)]