Skip to content

DF grouping

3 messages · Petr Savicky, karthicklakshman

#
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:
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.