Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

applying str_split_fixed function simultaneously across multiple columns in r

I have a data frame which is having multiple columns each column is having string values. I want to split column values by coma separator in the output data frame. Input and required output are as below

Col1=c("a,b,c","9,a,5")
Col2=c("c,b,e","4,r,t")
Col3=c("e,f,g","y,z,d")
Input=data.frame(Col1,Col2,Col3)

Column1=c("a","9")
Column2=c("b","a")
Column3=c("c","5")
Column4=c("c","4")
Column5=c("b","r")
Column6=c("e","t")
Column7=c("e","y")
Column8=c("f","z")
Column9=c("g","d")
Output=data.frame(Column1,Column2,Column3,Column4,Column5,Column6,Column7,Column8,Column9)

If anyone know the solution please help.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

The package splitstackshape is made for such operations,

library(splitstackshape)

cSplit(Input, names(Input), type.convert = 'as.is')

#   Col1_1 Col1_2 Col1_3 Col2_1 Col2_2 Col2_3 Col3_1 Col3_2 Col3_3
#1:      a      b      c      c      b      e      e      f      g
#2:      9      a      5      4      r      t      y      z      d
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading