R programming, concatenate or combine all the column contents
2
4
Entering edit mode
9.6 years ago
MAPK ★ 2.1k

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 • 15k views
ADD COMMENT
0
Entering edit mode

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 REPLY
6
Entering edit mode
9.6 years ago
alesssia ▴ 580
all <- paste(apply(df1, 1, function(row) paste(dQuote(row), collapse=",")), collapse=',"+",')
ADD COMMENT
0
Entering edit mode

Thank you very much!

ADD REPLY
4
Entering edit mode
9.6 years ago

You have to use the t (transpose) function.

> paste(dQuote(t(df1)), collapse=', ')
""chr4"," 443333"," 232444","+","chr5","4455332","4433323","-","chr5","4443333","4433355","+""
ADD COMMENT
0
Entering edit mode

Thank you, it works.

ADD REPLY
0
Entering edit mode

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

ADD REPLY

Login before adding your answer.

Traffic: 2106 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6