2

When I try to use ceiling() function, it works okay, but when I try to divide something and give it to the ceiling function using pipeline operator (2/10 %>% ceiling()), I get a problem.

ceiling(0.2)
1

ceiling(2/10)
1

2/10
0.2

2/10 %>% ceiling()
0.2

2 %>% `/`(10)
0.2

2 %>% `/`(10) %>% ceiling()
1

0.2 %>% ceiling()
1
Mehdi Abbassi
  • 627
  • 1
  • 7
  • 24

3 Answers3

3

Because 2/10 %>% ceiling() works as 2/(10 %>% ceiling()), i.e. %>% has precedence over /.

Put differently, 2/10 %>% ceiling() = 2/10 = 0.2

keepAlive
  • 6,369
  • 5
  • 24
  • 39
1

One magrittr solution could be:

2 %>%
 divide_by(10) %>%
 ceiling()

[1] 1
tmfmnk
  • 38,881
  • 4
  • 47
  • 67
1

It is because of operator precedence.

You can clarify what you want to do by using round or curly brackets

library(magrittr)
(2/10) %>% ceiling()

{2/10} %>% ceiling()
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213