Say there's a vector x:
假设有一个向量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:
我想快速地把它变成一个字符串a b。有没有一种不用循环的方法?我知道通过循环我可以做到:
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?
好像粘贴()可以巧妙地分割向量本身的元素,但我知道它不能。使用paste()的另一种功能或更有创意的方式能够有效地实现这一点吗?
1 个解决方案
#1
58
You just need to use the collapse
argument:
你只需要使用崩溃论点:
paste(x,collapse="")
#1
58
You just need to use the collapse
argument:
你只需要使用崩溃论点:
paste(x,collapse="")