R 如何在句子中打印向量以避免返回行

R 如何在句子中打印向量以避免返回行,r,vector,printf,cat,R,Vector,Printf,Cat,我想打印一个句子中向量的值。但是向量包含4个元素,这导致答案分布在4行上。这就是我所拥有的: yo = c(2902, 2908, 2907, 2918) cat(paste('You have', yo, 'number of individuals per species\n', sep = ' ')) You have 2902 number of individuals per species You have 2908 number of individuals per

我想打印一个句子中向量的值。但是向量包含4个元素,这导致答案分布在4行上。这就是我所拥有的:

  yo = c(2902, 2908, 2907, 2918)

  cat(paste('You have', yo, 'number of individuals per species\n', sep = ' '))

 You have 2902 number of individuals per species
 You have 2908 number of individuals per species
 You have 2907 number of individuals per species
 You have 2918 number of individuals per species
但是我想要这样的东西

You have 2902, 2908, 2907 and 2918 number of individuals per species
有可能这样做吗?此外,向量并不总是包含4个元素。如果我只有3或10个元素,它应该可以工作

这不起作用:

  cat(sprintf('You have %s number of individuals per species\n',yo))


  cat(paste('You have', unlist(yo), 'number of individuals per species\n', sep = ' '))
必须使用“折叠”选项粘贴:

paste('You have',paste( yo[1:(length(yo)-1)], collapse= ', '), 'and', yo[length(yo)], 'number of individuals per species\n')
You have 2902, 2908, 2907 and 2918 number of individuals per species
必须使用“折叠”选项粘贴:

paste('You have',paste( yo[1:(length(yo)-1)], collapse= ', '), 'and', yo[length(yo)], 'number of individuals per species\n')
You have 2902, 2908, 2907 and 2918 number of individuals per species
您可以使用toString和paste来获取最后一部分。我也用牛津逗号来衡量

x <- toString(c(yo[-length(yo)], paste("and", yo[length(yo)])))
x
# [1] "2902, 2908, 2907, and 2918"
您可以使用toString和paste来获取最后一部分。我也用牛津逗号来衡量

x <- toString(c(yo[-length(yo)], paste("and", yo[length(yo)])))
x
# [1] "2902, 2908, 2907, and 2918"

试着去尝试一下tringcyo[-lengthyo],pasteand,yo[lengthyo]。它使用牛津逗号。否则我会畏缩的。竖起大拇指看牛津逗号!试着去尝试一下tringcyo[-lengthyo],pasteand,yo[lengthyo]。它使用牛津逗号。否则我会畏缩的。竖起大拇指看牛津逗号!能否在最后一个值之前自动添加and而不是逗号?能否在最后一个值之前自动添加and而不是逗号?