user3346285
user3346285

Reputation: 101

How to split vector with strings

I have a vector with gene names where several elements in the vector contains more than one gene name, separated with a comma. How can I separate the elements of this vector and get a long vector with each gene name as a separate element of the vector? I have tried strsplit but that just give me the two or more gene names as separated strings but still in the same element of the vector... /Frida

genes = c("PGD", "CDA", "MROH7,TTC4", "PGM1") 

and I want to separate the element "MROH7,TTC4" into the two elements "MROH7" and "TTC4"

Upvotes: 2

Views: 190

Answers (2)

eipi10
eipi10

Reputation: 93761

This will split your string at every comma:

genes = c("PGD", "CDA", "MROH7,TTC4", "PGM1")
genes.split = unlist(strsplit(genes, ","))

genes.split
[1] "PGD"   "CDA"   "MROH7" "TTC4"  "PGM1" 

Upvotes: 9

Matthew Plourde
Matthew Plourde

Reputation: 44614

Another option is scan, which will also eat white space.

scan(text=genes, what='', sep=',', strip.white=TRUE)

Upvotes: 4

Related Questions