Question: R programming, concatenate or combine all the column contents
4
gravatar for MAPK
5.8 years ago by
MAPK1.7k
MAPK1.7k wrote:

 Hi Guys,

I want to concatenate all these values in the columns enclosed with "" and separated by comma in the order as shown below. How can I get this done in R?

df1

chr start end strand
chr4 443333 232444 +
chr5 4455332 4433323 -
chr5 4443333 4433355 +

I want this result

Result (exactly as shown below):

"chr4","443333","232444","+","chr5","4455332","4433323","-","chr5","4443333","4433355","+"

R • 12k views
ADD COMMENTlink modified 5.8 years ago • written 5.8 years ago by MAPK1.7k

Tried this, but didn't get what I want.

all<-paste(mydf[,"chr"],mydf[,"start"],mydf[,"end"],mydf[,"strand"], sep=",")
allkeys <- cat(paste(shQuote(all, type="cmd"), collapse=", "))

ADD REPLYlink modified 5.8 years ago • written 5.8 years ago by MAPK1.7k
6
gravatar for alesssia
5.8 years ago by
alesssia570
London, UK
alesssia570 wrote:
all <- paste(apply(df1, 1, function(row) paste(dQuote(row), collapse=",")), collapse=',"+",')

 

ADD COMMENTlink written 5.8 years ago by alesssia570

Thank you very much!

ADD REPLYlink written 5.8 years ago by MAPK1.7k
4
gravatar for Giovanni M Dall'Olio
5.8 years ago by
London, UK
Giovanni M Dall'Olio27k wrote:

You have to use the t (traspose) function.

> paste(dQuote(t(df1)), collapse=', ')
"“chr4”,“ 443333”,“ 232444”,“+”,“chr5”,“4455332”,“4433323”,“-”,“chr5”,“4443333”,“4433355”,“+”"
ADD COMMENTlink modified 5.8 years ago • written 5.8 years ago by Giovanni M Dall'Olio27k

Thank you, it works.

ADD REPLYlink modified 5.8 years ago • written 5.8 years ago by MAPK1.7k

Hi, I've edited the answer to include dQuote.

ADD REPLYlink written 5.8 years ago by Giovanni M Dall'Olio27k
Please log in to add an answer.

Help
Access

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.
Powered by Biostar version 2.3.0
Traffic: 1789 users visited in the last hour
_