Reputation: 3
I have a DF like the example created by the code below.
a = data.frame( name = c(rep("Tim",5),rep("John",3)),id = c(rep(1,5),rep(2,3)), value = 1:7)
And I want to transform it into a result that looks like this.
b = data.frame( name = c("Tim","John"), ID = c(1:2), b = NA)
b$value = list(c(1:5),c(6:8))
How would I go about doing this transformation?
For the actual data frame, I will have many columns to the left of the ID column, which I will want to perform calculations on with the columns of lists that will be created on the right side of the ID field.
For example, on the DF b above, I might want to perform a function call with "Tim" as an argument and loop through each individual element in the list = {1,2,3,4,5} and the output of that loop is another list with the same number of elements.
Upvotes: 0
Views: 50
Reputation: 886938
Try
aggregate(value~.,a, FUN=c)
# name id value
#1 Tim 1 1, 2, 3, 4, 5
#2 John 2 6, 7, 8
a <- structure(list(name = structure(c(2L, 2L, 2L, 2L, 2L, 1L, 1L,
1L), .Label = c("John", "Tim"), class = "factor"), id = c(1,
1, 1, 1, 1, 2, 2, 2), value = 1:8), .Names = c("name", "id",
"value"), row.names = c(NA, -8L), class = "data.frame")
Upvotes: 1