r - Dataframe manipulation: Convert certain columns of a dataframe into a list based on a key value column -
i have df example created 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 want transform result looks this.
b = data.frame( name = c("tim","john"), id = c(1:2), b = na) b$value = list(c(1:5),c(6:8))
how go doing transformation?
for actual data frame, have many columns left of id column, want perform calculations on columns of lists created on right side of id field.
for example, on df b above, might want perform function call "tim" argument , loop through each individual element in list = {1,2,3,4,5} , output of loop list same number of elements.
try
aggregate(value~.,a, fun=c) # name id value #1 tim 1 1, 2, 3, 4, 5 #2 john 2 6, 7, 8
data
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")
Comments
Post a Comment