Say there's a vector x:

x <- c("a", " ", "b")

and I want to quickly turn this into a single string "a b". Is there a way to do this without a loop? I know with a loop I could do this:

y <- ""
for (i in 1:3){
    paste(y, x[i], sep = "")
}

> y
[1] "a b"

but I will need to do this over many, many iterations, and having to loop over this and replace the original with the new each time would become very time consuming. I always want to be able to do something like this:

x <- paste(x)

as if paste() could smartly divide up the elements of a vector itself, but I know it can't. Is there another function, or a more creative way to use paste(), which can accomplish this efficiently?

推荐答案

You just need to use the collapse argument:

paste(x,collapse="")

R相关问答推荐

计算具有奇数日期的运行金额

在组中添加值增加和减少的行

将多列合并为单独的名称—值对

LOF中的插图短文字幕

R中的哈密顿滤波

使用for循环和粘贴创建多个变量

如何用书面利率绘制geom_bar图

如何在R中改变fviz_pca_biplot中圆的边界线的 colored颜色 ?

在R中,如何将误差条放置在堆叠的每个条上?

删除字符串R中的重复项

如果缺少时间,如何向日期-时间列添加时间

即使使用相同的种子,mtry值也取决于TuneGrid范围

使用离散标签自定义图例,用于具有连续但已入库的数据的热图

R直方图存储计算的bin值

按给定要求对向量进行排序

带有tidyModel框架的支持向量机的变量重要性图不起作用

Echarts4rBox函数似乎无法识别变量

根据字符串中的字符数将数据帧字符串列拆分为多个不同的列

变异以计算多个列的平均值,而无需将其打印出来

根据R中元素的索引,";CASE_WHEN&QOOT;?