-1
y <- data.frame(x = c("63,98,131","75,109,145","66,104,139"))

I want to make three columns A,B,C from x by splitting from comma

 A  B   C
 63 98  131
 75 109 145
 66 104 139

I tried to use str_split

str_split(y$x, " , ")

[[1]]
 [1] "63,98,131"

 [[2]]
 [1] "75,109,145"

  [[3]]
  [1] "66,104,139"

But this does not do the job. How can I fix it?

oguz ismail
  • 1
  • 16
  • 47
  • 69
89_Simple
  • 3,393
  • 3
  • 39
  • 94
  • You can use `tidyr::separate`. – Scarabee Mar 28 '18 at 00:08
  • 3
    This has been covered many, many times here - https://stackoverflow.com/questions/7069076/split-column-at-delimiter-in-data-frame - https://stackoverflow.com/questions/4350440/split-a-column-of-a-data-frame-to-multiple-columns - https://stackoverflow.com/questions/32042621/how-to-split-column-into-two-in-r-using-separate - and the various duplicates that each of these questions link to. – thelatemail Mar 28 '18 at 00:09
  • 1
    You need to do `","` not `" , "` – Mike H. Mar 28 '18 at 00:10

1 Answers1

0
> dt=as.data.frame(matrix(unlist(strsplit(y$x,",")),ncol=dim(y)[1],byrow = T))
> dt
  V1  V2  V3
1 63  98 131
2 75 109 145
3 66 104 139