如何在 paste() 语句中包含具有多个元素的向量?
How to include a vector with multiple elements in a paste() statement?
有一个名为 error.list 的字符向量,其中包含多个元素。我试图在我的粘贴语句中包含完整的矢量,如下所示:
paste("Hi, Your data has the following errors:", error.list," Regards, XYZ",
sep='').
但是我遇到了一个错误。控制台说它不能在单个粘贴语句中添加所有元素。可以帮我解决这个问题吗?还有其他方法吗?
您的方法可行 For inputs of length 1
但您有一个向量。
Because error.list
is a vector
, use one of the following methods:
error.list <- c("a", "b", "c")
paste(c("Hi, Your data has the following errors:", error.list, " Regards, XYZ"), sep = " ")
Provide the output:
[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 = " ")
Provide the output:
"Hi, Your data has the following errors: a b c Regards, XYZ"
or you can use paste0()
with parameter collapse =
to get one line output, for also using the error.list
as vector:
paste0(c("Hi, Your data has the following errors:", error.list, " Regards, XYZ"), collapse = " ")
Provide the output:
[1] "Hi, Your data has the following errors: a b c Regards, XYZ"
有一个名为 error.list 的字符向量,其中包含多个元素。我试图在我的粘贴语句中包含完整的矢量,如下所示:
paste("Hi, Your data has the following errors:", error.list," Regards, XYZ",
sep='').
但是我遇到了一个错误。控制台说它不能在单个粘贴语句中添加所有元素。可以帮我解决这个问题吗?还有其他方法吗?
您的方法可行 For inputs of length 1
但您有一个向量。
Because
error.list
is avector
, use one of the following methods:
error.list <- c("a", "b", "c")
paste(c("Hi, Your data has the following errors:", error.list, " Regards, XYZ"), sep = " ")
Provide the output:
[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 = " ")
Provide the output:
"Hi, Your data has the following errors: a b c Regards, XYZ"
or you can use
paste0()
with parametercollapse =
to get one line output, for also using theerror.list
as vector:
paste0(c("Hi, Your data has the following errors:", error.list, " Regards, XYZ"), collapse = " ")
Provide the output:
[1] "Hi, Your data has the following errors: a b c Regards, XYZ"