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 ab 1,2,5 2 cd 3,5,7 3 ef 9,8 4 gh 1
(The third column is a text column with comma separated values.) I would like to convert it to a data frame as follows:
ab 1 ab 2 ab 5 cd 3 cd 5 cd 7 ef 9 ef 8 gh 1
Can anyone suggest a way to do this using an application? I use the command below closely, but this is not entirely correct. Any suggestions on more effective ways to do this would also be appreciated ...
> 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"