Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R data transformation

Tags:

r

I have an R data frame that looks like this:

z = as.data.frame(list(Col1=c("a","c","e","g"),Col2=c("b","d","f","h"),Col3=c("1,2,5","3,5,7","9,8","1")))
> z
  Col1 Col2  Col3
1    a    b 1,2,5
2    c    d 3,5,7
3    e    f   9,8
4    g    h     1

(The third column is a text column with comma-separated values.) I would like to convert it to a data frame like this:

a    b    1
a    b    2
a    b    5
c    d    3
c    d    5
c    d    7
e    f    9 
e    f    8
g    h    1

Can anyone suggest a way to accomplish this using apply? I'm close using the command below but it's not quite right. Any suggestions on more efficient ways to do this would be appreciated as well...

> apply(z,1,function(a){ids=strsplit(as.character(a[3]),",")[[1]];out<-c();for(id in ids){out<-rbind(out,c(a[1:2],id))};return(out)})
[[1]]
     Col1 Col2    
[1,] "a"  "b"  "1"
[2,] "a"  "b"  "2"
[3,] "a"  "b"  "5"

[[2]]
     Col1 Col2    
[1,] "c"  "d"  "3"
[2,] "c"  "d"  "5"
[3,] "c"  "d"  "7"

[[3]]
     Col1 Col2    
[1,] "e"  "f"  "9"
[2,] "e"  "f"  "8"

[[4]]
     Col1 Col2    
[1,] "g"  "h"  "1"
like image 530
Andrew Avatar asked Feb 22 '23 08:02

Andrew


1 Answers

You can use ddply.

library(plyr)
ddply(z, c("Col1", "Col2"), summarize, 
  Col3=strsplit(as.character(Col3),",")[[1]]
)
like image 174
Vincent Zoonekynd Avatar answered Mar 02 '23 18:03

Vincent Zoonekynd