如何在R中正确嵌套paste()函数?

如何在R中正确嵌套paste()函数?,r,R,你能告诉我为什么吗 paste(paste(c("first", "second"), collapse=", "), "third", collapse=" and ") 给我 "first, second third" 而不是 "first, second and third" ??获取第二个输出的正确语法是什么?或使用: paste(paste(c("first", "second"), collapse=", "), "third", sep=", and ") ## "first

你能告诉我为什么吗

paste(paste(c("first", "second"), collapse=", "), "third", collapse=" and ")
给我

"first, second third"
而不是

"first, second and third"
??获取第二个输出的正确语法是什么?

或使用:

paste(paste(c("first", "second"), collapse=", "), "third", sep=", and ")
## "first, second, and third"
请参阅关于粘贴的这篇博文:

或使用:

paste(paste(c("first", "second"), collapse=", "), "third", sep=", and ")
## "first, second, and third"
请看这篇关于粘贴的博文:

让我们将其分解:

第一部分:

> paste(c("first", "second"), collapse=", ")
#[1] "first, second"
第二部分:

> paste("first, second", "third", collapse=" and ")
#[1] "first, second third"
使用sep=而不是collapse=

> paste("first, second","third",sep=" and ")
#[1] "first, second and third"
因此,您可以使用:

> paste(paste(c("first", "second"), collapse=", "), "third",sep=", and ")
#[1] "first, second, and third"
让我们把它分解一下:

第一部分:

> paste(c("first", "second"), collapse=", ")
#[1] "first, second"
第二部分:

> paste("first, second", "third", collapse=" and ")
#[1] "first, second third"
使用sep=而不是collapse=

> paste("first, second","third",sep=" and ")
#[1] "first, second and third"
因此,您可以使用:

> paste(paste(c("first", "second"), collapse=", "), "third",sep=", and ")
#[1] "first, second, and third"
解释 在调用函数的方式上有细微的差别。在

paste(c("first", "second"), collapse = ", ")
请注意,您只通过长度为2的向量
传递一个参数。这将导致返回长度为1的向量:

> paste(c("first", "second"), collapse = ", ")
[1] "first, second"
然后通过外部
粘贴()
中的
..
传递此长度1向量和第二个长度1向量。实际上,你正在做:

paste("first, second", "third", collapse = " and ")
由于提供的两个向量的长度均为1,因此没有任何可折叠的内容,这两个字符串只是用分隔符
sep
连接起来

因此,在第一种情况下

paste(c("first", "second"), collapse = ", ")
没有第二个向量可以将值粘贴到第一个向量上,因此
paste()
使用
“,”
作为分隔符折叠单个向量的元素
c(“第一”、“第二”)
。但当你传入两个长度为1的向量时

paste("first, second", "third", collapse = " and ")
该函数使用
sep
将两个字符串连接成一个字符串
“first,second-third”
,然后由于只有一个长度为1的字符,因此没有任何可折叠的内容

特定溶液 如果您只需要字符串
“first、second和third”
,并且您的输入与您的示例相同,只需切换到使用
sep=“”和“
”:

paste(paste(c("first", "second"), collapse=", "), "third", sep = ", and ")

> paste(paste(c("first", "second"), collapse=", "), "third", sep = ", and ")
[1] "first, second, and third"

如果你不想要牛津逗号

总结 查看
sep
collapse
如何与提供的输入相关的最简单方法。两个例子是说明性的:

> paste(1:5, letters[1:5], LETTERS[1:5], sep = " ")
[1] "1 a A" "2 b B" "3 c C" "4 d D" "5 e E"
> paste(1:5, letters[1:5], LETTERS[1:5], sep = " ", collapse = "-")
[1] "1 a A-2 b B-3 c C-4 d D-5 e E"
我们观察到两个特点:

  • sep
    对输入向量应用元素,例如对
    1
    a
    a
    ;然后是
    2
    b
    b
    ;等等
  • collapse
    应用于使用
    sep
    的第一个粘贴步骤的结果,如果通过将元素与参数
    collapse
    给定的分隔符串联,该结果会导致长度>=2的向量。如果没有
    collapse
    paste()
    可以在给定向量输入的情况下返回多个字符串的向量,但是如果指定了
    collapse
    ,则只会返回长度为1的字符向量
  • 解释 在调用函数的方式上有细微的差别。在

    paste(c("first", "second"), collapse = ", ")
    
    请注意,您只通过长度为2的向量
    传递一个参数。这将导致返回长度为1的向量:

    > paste(c("first", "second"), collapse = ", ")
    [1] "first, second"
    
    然后通过外部
    粘贴()
    中的
    ..
    传递此长度1向量和第二个长度1向量。实际上,你正在做:

    paste("first, second", "third", collapse = " and ")
    
    由于提供的两个向量的长度均为1,因此没有任何可折叠的内容,这两个字符串只是用分隔符
    sep
    连接起来

    因此,在第一种情况下

    paste(c("first", "second"), collapse = ", ")
    
    没有第二个向量可以将值粘贴到第一个向量上,因此
    paste()
    使用
    “,”
    作为分隔符折叠单个向量的元素
    c(“第一”、“第二”)
    。但当你传入两个长度为1的向量时

    paste("first, second", "third", collapse = " and ")
    
    该函数使用
    sep
    将两个字符串连接成一个字符串
    “first,second-third”
    ,然后由于只有一个长度为1的字符,因此没有任何可折叠的内容

    特定溶液 如果您只需要字符串
    “first、second和third”
    ,并且您的输入与您的示例相同,只需切换到使用
    sep=“”和“
    ”:

    paste(paste(c("first", "second"), collapse=", "), "third", sep = ", and ")
    

    > paste(paste(c("first", "second"), collapse=", "), "third", sep = ", and ")
    [1] "first, second, and third"
    

    如果你不想要牛津逗号

    总结 查看
    sep
    collapse
    如何与提供的输入相关的最简单方法。两个例子是说明性的:

    > paste(1:5, letters[1:5], LETTERS[1:5], sep = " ")
    [1] "1 a A" "2 b B" "3 c C" "4 d D" "5 e E"
    > paste(1:5, letters[1:5], LETTERS[1:5], sep = " ", collapse = "-")
    [1] "1 a A-2 b B-3 c C-4 d D-5 e E"
    
    我们观察到两个特点:

  • sep
    对输入向量应用元素,例如对
    1
    a
    a
    ;然后是
    2
    b
    b
    ;等等
  • collapse
    应用于使用
    sep
    的第一个粘贴步骤的结果,如果通过将元素与参数
    collapse
    给定的分隔符串联,该结果会导致长度>=2的向量。如果没有
    collapse
    paste()
    可以在给定向量输入的情况下返回多个字符串的向量,但是如果指定了
    collapse
    ,则只会返回长度为1的字符向量

  • 我做了一个调整,以表明
    sep
    不仅仅是成对应用,并改变了我认为的打字错误。如果这不符合规定,请恢复编辑并让我知道。@BlueMagister感谢您的帮助;我尝试添加第三个向量,或者想知道是否可以按字面理解成对向量,而不是在本例中。因为这已经是一个很长的答案了,所以简单化了,但是现在你的编辑是一个更好的解释+1我做了一个调整,以表明
    sep
    不仅仅是成对应用,并改变了我认为的打字错误。如果这不符合规定,请恢复编辑并让我知道。@BlueMagister感谢您的帮助;我尝试添加第三个向量,或者想知道是否可以按字面理解成对向量,而不是在本例中。因为这已经是一个很长的答案了,所以简单化了,但是现在你的编辑是一个更好的解释+1.