Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Paste to collapse a vector in R

Tags:

r

paste

I have a data frame mynames of a single column that i wish to convert to a character string. The aim being to select the names of columns in another dataset based on the items in this string. An example of the data is below

X
A
B
C

I tried using the code below to paste it all together using the following code

paste(mynames, sep="", collapse="")

However that produces the out put below which is far from ideal

"c(\"A\", \"B\", \"C\",..........

Can anyone help?

like image 268
John Smith Avatar asked Oct 29 '25 09:10

John Smith


1 Answers

Try

dat <- data.frame(x=c("a","b","c"))
st <- paste(dat$x,collapse="")
st

prints

> st
[1] "abc"

and to make it comma-separated:

> st <- paste(dat$x,collapse=",")
> st
[1] "a,b,c"
like image 56
rbm Avatar answered Oct 31 '25 00:10

rbm



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!