Chain arithmetic operators in dplyr with %>% pipe

agenis

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.

jbaums

Surround the operators with backticks or quotes, and things should work as expected:

1:10 %>%  `*`(2) %>% sum
# [1] 110

1:10 %>%  `/`(2) %>% sum
# [1] 27.5

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Using dplyr, how to pipe or chain to plot()?

From Dev

Understanding arithmetic operators in Python

From Dev

Arithmetic on two CGPoints with + or - operators

From Dev

Treat arithmetic operators as functions

From Dev

Array of Arithmetic Operators in Swift

From Dev

Calculation program with arithmetic operators

From Dev

Storing and using arithmetic operators

From Dev

Javascript:Inconsistency with arithmetic operators?

From Dev

Trouble with arithmetic operators in AngularJS

From Dev

Chain Increment Operators

From Dev

/windowApache Flink chain operators

From Dev

RxJava operators or Kotlin chain

From Dev

Are arithmetic operators ever preferable to arithmetic functions?

From Dev

Is there an equivalent to optional chaining with arithmetic operators?

From Dev

"Simple" Expression Language - arithmetic operators?

From Dev

"Simple" Expression Language - arithmetic operators?

From Dev

How to modify arithmetic operators (+,-,x)

From Dev

Action in controller not accepting arithmetic operators

From Dev

overloading arithmetic operators c++

From Dev

Copying a variable with only arithmetic operators

From Dev

replace NA in a dplyr chain

From Java

Removing NA in dplyr pipe

From Dev

Dplyr pipe (%>%) within mutate()?

From Dev

How to avoid NullPointerException from arithmetic operators in Java?

From Dev

Perform arithmetic operations from operators defined as strings

From Dev

Java operators performance arithmetic vs bitwise

From Dev

Using arithmetic operators and/or brackets in a LIMIT clause

From Dev

How to pass arithmetic operators as function parameters in LISP?

From Dev

How to use arithmetic operators to get cell location

Related Related

HotTag

Archive