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

How to separate strings of a column and store them in a vector

I have a dataframe similar to data (see example below). I would like to create a vector containing all the string characters of IIIF separated by a comma as in out.

data=data.frame(IIIT=c("a", "b", "c", "d", "e", "f", "g"), IIIF=c("aze,hyt,fre", NA, "ade", "ijh, deg","oij,erf", "eft,kij", "efg,kijj,lerod,kjhyg"))

> data
  IIIT                 IIIF
1    a          aze,hyt,fre
2    b                 <NA>
3    c                  ade
4    d             ijh, deg
5    e              oij,erf
6    f              eft,kij
7    g efg,kijj,lerod,kjhyg

> out
 [1] "aze"   "hyt"   "fre"   NA      "ade"   "ijh"   "deg"   "oij"   "erf"   "eft"   "kij"   "efg"   "kijj"  "lerod" "kjhyg"



How can I do that ?

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 :

Base R has strsplit() which will create a list, with each element of the list being a character vector of each separate word found in the original vector. You can then combine the results using unlist():

> unlist(strsplit(data$IIIF, split = ","))
 [1] "aze"   "hyt"   "fre"   NA      "ade"   "ijh"   " deg"  "oij"   "erf"  
[10] "eft"   "kij"   "efg"   "kijj"  "lerod" "kjhyg"
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