How do I properly nest paste() functions in R?

后端 未结 3 610
孤街浪徒
孤街浪徒 2021-01-27 00:16

Can you tell me why

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

gives me

\"first,          


        
3条回答
  •  借酒劲吻你
    2021-01-27 00:41

    Let's break it down:

    1st part:

    > paste(c("first", "second"), collapse=", ")
    #[1] "first, second"
    

    2nd part:

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

    using sep= instead of collapse=

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

    Therefore you can use:

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

提交回复
热议问题