Search code examples
rdplyrnumericpiping

Numeric calculations using dplyr piping commands


Is it possible to use piping from the dplyr package to do numeric calculations? A simple example is:

    0.15 %>% 3.8416 * (.*(1-.))/(0.03^2)  #does not work
    seq(1,10,1) %>% log(.) %>% .^2        #works

Tying to understand more of how piping works and when it can and cannot be used. I really enjoy using the piping feature and want to find a way to use it for these types of numeric calculations.

Many thanks


Solution

  • You have an operator precedence problem. It's trying to start by doing

    0.15 %>% 3.8416
    

    which doesn't make sense. You need to group all your calculations in a code block

    0.15 %>% {3.8416 * (.*(1-.))/(0.03^2)}