Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What do %/% and %% mean?

Tags:

r

dplyr

I've recently seen code like this:

library(dplyr)
mtcars %.% mutate(carb_10 =  carb %/% 10)

And this....

mtcars %.% mutate(carb_10 =  carb %% 10)

Can anyone explain what %/% and %% do in above code?

like image 215
luciano Avatar asked Feb 18 '26 18:02

luciano


2 Answers

In R:

  • %% is the modulo operator
  • example: 5 %% 3 will be equal to 2 as it is the remainder obtained on the integral division of 5 by 3

In Python, you can do it by simply using: %

  • 5 % 3

In R:

  • %/% is the integer division operator
  • example: 5 %/% 3 will be equal to 1 as it is the quotient obtained on the integer division of 5 by 3

In Python, you can do it by simply using: //

  • 5 // 3
like image 140
kush_shah Avatar answered Feb 20 '26 06:02

kush_shah


From the ?"%%" help page

%% indicates x mod y and %/% indicates integer division. It is guaranteed that x == (x %% y) + y * ( x %/% y ) (up to rounding error) unless y == 0 where the result of %% is NA_integer_ or NaN (depending on the typeof of the arguments).

like image 34
MrFlick Avatar answered Feb 20 '26 08:02

MrFlick



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!