Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

dplyr pipe input processing?

Tags:

r

dplyr

magrittr

I have not been able to find an answer to this question, so I want to pose it here. Does anyone know what is going on here?

as.integer(.29*100)
[1] 28

.29*100 %>% as.integer
[1] 29

I understand that .29*100 would be a double and doubles cannot be perfectly represented, hence why we get that output from as.integer since it just casts the double to an int, but what is it about the pipe which is making the result different?

like image 826
John Snyder Avatar asked Dec 06 '25 16:12

John Snyder


1 Answers

We need the parens () to keep it as a single block otherwise there is an operator precedence

library(magrittr)
(.29 * 100) %>%
      as.integer
#[1] 28

i.e. it is doing

as.integer(100) * 0.29
#[1] 29

We can also do some versions of the above if there is any difficulty in wrapping with parens

.29 %>%
    `*`(100) %>% 
   as.integer
#[1] 28

or use the alias multiply_by

.29 %>%
    multiply_by(100) %>%
   as.integer
#[1] 28
like image 165
akrun Avatar answered Dec 08 '25 08:12

akrun



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!