How to separate comma separated values in R in a new row? -


i have dataset follow:

col1    col2        1,2,3 b        ["1","2"] c        4 

i want output as:

col1     col2         1         2         3 b         1 b         2 c         4 

is possible in r? if yes, how?

you try csplit "splitstackshape" package:

library(splitstackshape) csplit(as.data.table(mydf)[, col2 := gsub("[][\"]", "", col2)],         "col2", ",", "long") #    col1 col2 # 1:       1 # 2:       2 # 3:       3 # 4:    b    1 # 5:    b    2 # 6:    c    4 

of course, i'm highly partial csplit, can use "dplyr" , unnest "tidyr":

library(dplyr) library(tidyr)  mydf %>%   mutate(col2 = strsplit(gsub("[][\"]", "", col2), ",")) %>%   unnest(col2) 

or "data.table":

library(data.table) as.data.table(mydf)[, list(   col2 = unlist(strsplit(gsub("[][\"]", "", col2), ","))),    = col1] 

Comments

Popular posts from this blog

Hatching array of circles in AutoCAD using c# -

ios - UITEXTFIELD InputView Uipicker not working in swift -

Python Pig Latin Translator -