[英]Using Paste to collapse a vector in R
I have a data frame mynames
of a single column that i wish to convert to a character string.我有一个
mynames
的数据框mynames
,我希望将其转换为字符串。 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\\",.......... "c(\\"A\\", \\"B\\", \\"C\\",........
Can anyone help?任何人都可以帮忙吗?
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"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.