Hello Members, I need to group a data.frame in a a specific way, like shown below, DF raw is like this, id col1 col2 score 1 A B 40 2 B C 55 3 C D 4000 4 D E 100 5 E F 300 I want the out put as List [1] A B C [2] D E [3] F Basically the split should be based on the DF$score > 200, and all the col1 and col2 values should be aggrigated. Request your kind help, Regards, karthick -- View this message in context: http://r.789695.n4.nabble.com/DF-grouping-tp4381310p4381310.html Sent from the R help mailing list archive at Nabble.com.
On Sun, Feb 12, 2012 at 07:07:26AM -0800, karthicklakshman wrote:> Hello Members, > > I need to group a data.frame in a a specific way, like shown below, > > DF raw is like this, > > id col1 col2 score > 1 A B 40 > 2 B C 55 > 3 C D 4000 > 4 D E 100 > 5 E F 300 > > I want the out put as > > List > [1] > A B C > [2] > D E > [3] > F > Basically the split should be based on the DF$score > 200, and all the col1 > and col2 values should be aggrigated.Hi. Is it always true that DF[i, "col2"] == DF[i+1, "col1"]? If yes, then try the following # dput() of DF DF <- structure(list(col1 = structure(1:5, .Label = c("A", "B", "C", "D", "E"), class = "factor"), col2 = structure(1:5, .Label = c("B", "C", "D", "E", "F"), class = "factor"), score = c(40L, 55L, 4000L, 100L, 300L)), .Names = c("col1", "col2", "score"), class = "data.frame", row.names = c(NA, -5L)) # linearize col1, col2 objects <- c(as.character(DF[1, "col1"]), as.character(DF[, "col2"])) scores <- c(0, DF[, "score"]) # output as list of vectors out1 <- split(objects, cumsum(scores > 200)) out1 $`0` [1] "A" "B" "C" $`1` [1] "D" "E" $`2` [1] "F" # output as list of character strings out2 <- lapply(out1, paste, collapse=" ") out2 $`0` [1] "A B C" $`1` [1] "D E" $`2` [1] "F" Hope this helps. Petr Savicky.
Dear Petr Savicky, Thank you very much for the solution. your script really worked for my DF, and yes as per your guess , its always ---> DF[i, "col2"] == DF[i+1, "col1"] Regards, karthick -- View this message in context: http://r.789695.n4.nabble.com/DF-grouping-tp4381310p4381646.html Sent from the R help mailing list archive at Nabble.com.
Hello Petr Savicky, hello all, I have a situation similar to the previous one, I need to group a data.frame in a specific way, col1 col2 score 2873 3192 319 4268 4451 183 5389 5534 145 6622 10622 4000 12631 17853 5222 20408 20615 207 21595 21838 243 23121 23139 18 the out put should be like, [1] 2873 3192 4268 4451 5389 5534 6622 10622 [2] 10622 12631 [3] 17853 20408 20615 21595 21838 23121 23139 Basically the split should be based on the DF$score > 500, and all the col1 and col2 values should be aggregated. but DF[i, "col2"] != DF[i+1, "col1"] Your inputs will be very helpful Thank you Karthick -- View this message in context: http://r.789695.n4.nabble.com/DF-grouping-tp4381310p4650865.html Sent from the R help mailing list archive at Nabble.com.