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 modular division, 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.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…