This is a test version of Biostars. For the public version, visit https://www.biostars.org.
R programming, concatenate or combine all the column contents

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

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=", "))

2 answers

all <- paste(apply(df1, 1, function(row) paste(dQuote(row), collapse=",")), collapse=',"+",')

Thank you very much!

You have to use the t (transpose) function.

> paste(dQuote(t(df1)), collapse=', ')
""chr4"," 443333"," 232444","+","chr5","4455332","4433323","-","chr5","4443333","4433355","+""

Thank you, it works.

Log in to answer this question.