Data conversion 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 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" 
+4
source share
2 answers

With reshape or reshape2

 require(reshape2) merge(cbind(z[,-3], L1=rownames(z)), melt(strsplit(as.character(z$Col3),","))) 

gives

  L1 Col1 Col2 value 1 1 ab 1 2 1 ab 2 3 1 ab 5 4 2 cd 3 5 2 cd 5 6 2 cd 7 7 3 ef 9 8 3 ef 8 9 4 gh 1 
+3
source

You can use ddply .

 library(plyr) ddply(z, c("Col1", "Col2"), summarize, Col3=strsplit(as.character(Col3),",")[[1]] ) 
+5
source

Source: https://habr.com/ru/post/1398167/


All Articles