如何在paste()语句中包含具有多个元素的向量?

时间:2018-10-28 20:41:09

标签: r rstudio-server

具有一个名为error.list的字符向量,其中包含多个元素。我试图在我的粘贴语句中包括完整的向量,如下所示:

paste("Hi, Your data has the following errors:", error.list," Regards, XYZ", 
sep=''). 

但是我遇到一个错误。控制台表示无法在单个粘贴语句中添加所有元素。可以帮我吗?还有另一种方法吗?

1 个答案:

答案 0 :(得分:1)

Concatenate Strings

您的方法将For inputs of length 1有效,但是您有一个方向。

  

由于error.listvector,请使用以下方法之一:

error.list <- c("a", "b", "c")

paste(c("Hi, Your data has the following errors:", error.list, " Regards, XYZ"), sep = " ")
  

提供输出:

[1] "Hi, Your data has the following errors:" "a"                                      
[3] "b"                                       "c"                                      
[5] " Regards, XYZ"

一行使用参数collapse =

paste(c("Hi, Your data has the following errors:", error.list, " Regards, XYZ"), sep = "  ", collapse = " ")
  

提供输出:

"Hi, Your data has the following errors: a b c  Regards, XYZ"

  

或者您可以将paste0()与参数collapse =一起使用以获取一行输出,也可以将error.list用作向量:

paste0(c("Hi, Your data has the following errors:", error.list, " Regards, XYZ"), collapse = " ")    
  

提供输出:

     

[1] "Hi, Your data has the following errors: a b c Regards, XYZ"