用向量粘贴文本

时间:2015-06-02 13:15:25

标签: r

我正在尝试使用向量粘贴一些文本:

v1 <- c(16,17,18)
paste ("the numbers in v1 are:",v1) 

我想得到:     v1中的数字是:16,17,18 但相反它得到:

[1] "the numbers in v1 are: 16" "the numbers in v1 are: 17"
[3] "the numbers in v1 are: 18"

对于我做错了什么,我会很感激。

2 个答案:

答案 0 :(得分:5)

您需要先崩溃v1

paste("the numbers in v1 are:", paste(v1, collapse = ","))
# [1] "the numbers in v1 are: 16,17,18"

您的代码将首先重复"the numbers in v1 are",直到它与v1的长度匹配,然后它将按索引将这些向量的元素粘贴在一起。

或者

sprintf('the numbers in v1 are: %s', toString(v1))
# [1] "the numbers in v1 are: 16, 17, 18"

答案 1 :(得分:1)

不太对,但它实现了目标。

cat("the numbers in v1 are:", v1)