I would like to understand why, in the the dplyr or magrittr package, and more specifically the chaining function %>% has some trouble with the basic operators +, -, *, and /
Chaining takes the output of previous statement and feeds it as first argument of the next:
1:10 %>% sum
# [55]
Thus how come this doesn't work
1:10 %>% *2 %>% sum
1:10 %>% .*2 %>% sum
I also found that the following syntax works for adding/substracting, but not multiply or divide. why so?
1:10 %>% +(2) # works OK
1:10 %>% *(2) # nope...
So should I write an anonymous function even to do a *2 operation on my data.frame?
1:10 %>% (function(x) x*2) %>% sum
Thanks, I couldn't find the answer in other SO questions.
magrittrpackage here instead of dplyr. dplyr is only for working with data.frames while the pipe operator (%>%) is originally from magrittr. - talat-2. So its valid syntax. So it parses okay here, and then magrittr gets to work mangling the evaluation into a binary"-"(x, 2)expression. There's no unary '*' or '/' function, so those ops fail. Once quoted, they become valid syntax again and the corresponding function is gotten. - Spacedman