使用paste0的中缀(%>%)运算符

时间:2017-11-21 15:28:41

标签: r magrittr

所以我想在%>% way中堆叠字符串。说,我有一个字符向量

names <- c("Alice","Bob","Charles")

和一个字符串:

phrase <- "Name is "

通常的方法是paste0

> paste0(phrase, names)
[1] "Name is Alice"   "Name is Bob"     "Name is Charles"

有没有办法用%>%执行此操作?我能够以相反的顺序连接这些:

> names %>% paste0(phrase)
[1] "AliceName is "   "BobName is "     "CharlesName is "

1 个答案:

答案 0 :(得分:5)

您可以尝试:

phrase %>% paste0(names)

哪个给出了

[1] "Name is Alice"   "Name is Bob"     "Name is Charles"

或者,您可以使用.

访问LHS
names %>% paste0(phrase, .)

还会返回:

[1] "Name is Alice"   "Name is Bob"     "Name is Charles"