2

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"

2
  • 3
    Welcome on SO. Could you please provide an reproducible example. Commented May 21, 2014 at 19:42
  • Hi,the vector looks like this: genes = ("PGD", "CDA", "MROH7,TTC4", "PGM1") and I want to separate the element "MROH7,TTC4" into the two elements "MROH7" and "TTC4". Commented May 21, 2014 at 19:45

2 Answers 2

9

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" 
Sign up to request clarification or add additional context in comments.

Comments

4

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

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.