What does the double percentage sign (%%) mean?

The "Arithmetic operators" help page (which you can get to via ?"%%") says

%%’ indicates ‘x mod y’

which is only helpful if you've done enough programming to know that this is referring to the modulo operation, i.e. integer-divide x by y and return the remainder. This is useful in many, many, many applications. For example (from @GavinSimpson in comments), %% is useful if you are running a loop and want to print some kind of progress indicator to the screen every nth iteration (e.g. use if (i %% 10 == 0) { #do something} to do something every 10th iteration).

Since %% also works for floating-point numbers in R, I've just dug up an example where if (any(wts %% 1 != 0)) is used to test where any of the wts values are non-integer.


The result of the %% operator is the REMAINDER of a division,

Eg. 75%%4 = 3

I noticed if the dividend is lower than the divisor, then R returns the same dividend value.

Eg. 4%%75 = 4

Cheers

division parts

Tags:

R