This is a test version of Biostars. For the public version, visit https://www.biostars.org.
make a vector containing several vectors in to a single one

I extract a column from CSV containing geneName from geneOntology analysis. The result is a vector that contains several sub-vectors.

Now I want to make all elements into a single vector instead of a vector containing several vectors, what is the convenient way to do this?

Thank you,

r

Data examples please, words alone are typically not sufficient.

geneOntology <- read.csv("IRE11_combind_E18_David_Direct_BP.csv", stringsAsFactors = FALSE)
genes <- geneOntology$Genes[geneOntology$Group == "E18>E11"]
genes
[1] "ERCC6L2, CDC14B, XRCC6, PRKDC, INO80, BCCIP, SPRTN, DMAP1, KIN, MUTYH, ERCC6, MDC1, ACTR5, POLM, HINFP, USP10, ACTR8, FANCG, RTEL1, SLX1B, EME2, NFRKB, LIG1, NEIL1, LIG3, PNKP, USP28, RPAIN, TDP2, ABL1, PARP2"                                                                                                                                                                                                                                                                                                                                                                                                                                                
 [2] "GTPBP4, WBSCR22, ERAL1, IPO9, BMS1, NVL, DROSHA, IPO4, TSR3, ZNHIT6, UTP14A, SETD4, FTSJ3, GNL3, DDX51"                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          
 [3] "RECQL4, SLX1B, EME2, LIG1, NFRKB, XRCC6, LIG3, INO80, KIN, RPAIN, ACTR5, POLM, ACTR8, RTEL1"

In this case the genes contains 3 sub-vectors. How to fuse the 3 sub-vectors directly into 1?

Probably unlist()

I tried it. It works on list. I even tried to use as.list to transform the genes into a list and then do unlist. Still does not work

How about

unlist(lapply(genes, function(x)x))
y <- unlist(lapply(genes, function(x)x))
> y[1]
[1] "ERCC6L2, CDC14B, XRCC6, PRKDC, INO80, BCCIP, SPRTN, DMAP1, KIN, MUTYH, ERCC6, MDC1, ACTR5, POLM, HINFP, USP10, ACTR8, FANCG, RTEL1, SLX1B, EME2, NFRKB, LIG1, NEIL1, LIG3, PNKP, USP28, RPAIN, TDP2, ABL1, PARP2"

Looks still not working.

1 answer

it is difficult to help since you provide no copy/paste ready data, try

gsub(" ", "", unlist(sapply(strsplit(genes, split = ","), function(x)x)))

and if this does not work then please use dput to provide the data so we can reproduce things.

It worked! I will learn to use dput to provide data in the future post!

Appreciate a lot!

Log in to answer this question.