Search code examples
rmagrittr

Infix (%>%) operator with paste0


So I want to stack strings in %>% way. Say, I have a character vector

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

and a string:

phrase <- "Name is "

A usual way to do this is with paste0:

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

Is there a way to do this with %>%? I am able to concatenate these only in reversed order:

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

Solution

  • You can try:

    phrase %>% paste0(names)
    

    Which gives

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

    Or, you can access the LHS by using .:

    names %>% paste0(phrase, .)
    

    Which also returns:

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