Search code examples
rdplyrmagrittr

What is wrong with this line of code with pipe %>%?


I'm trying to run.

1:10 %>% replace(`>`(.,4), 999)
Error in replace(`1:10`, . > 4, 999) : object '.' not found

Maybe I've been using . wrong all this time, so I go read the magrittr documentation. I learned about using {} to override passing . as the first argument.

So now I try

1:10 %>% {replace(., `>`(.,4), 999)}
Error in replace(`1:10`, . > 4, 999) : object '.' not found

Nope, still doesn't work. I run the example code given for {} (page 9)

1:10 %>% {c(min(.), max(.))}
Error in eval(expr, envir, enclos) : object '.' not found

Example code doesn't even work for me. What am I doing wrong?


Solution

  • I'm with @Akrun in not being able to reproduce the error, but

    1:10 %>% replace(.>4,999)
    

    seems simpler and easier to read. (This is in a clean session with magrittr version 1.5, no other non-base packages loaded, nothing else in the workspace.)

    Side comment: I assume this is part of a longer/more complicated workflow. If you're just replacing replace(x,x>4,999) with x %>% replace(.>4,999) I'm not sure I see the point ...