Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

r

What is the double percent (%%) used for in R?

From using it, it looks as if it divides the number in front by the number in back of it as many times as it can and returns the left over value. Is that correct?

Out of curiosity, when would this be useful?

like image 401
pheeper Avatar asked May 15 '15 10:05

pheeper


People also ask

What does double percent mean?

An increase of 100% in a quantity means that the final amount is 200% of the initial amount (100% of initial + 100% of increase = 200% of initial). In other words, the quantity has doubled.

What does %% mean in R language?

%% gives Remainder. %/% gives Quotient. So 6 %% 4 = 2. In your example b %% a , it will vectorised over the values in “a”

What is the difference between %% and %/% in R?

%% indicates x mod y and %/% indicates integer division.

What does double percentage sign mean in Python?

The "%%" does not have special meaning inside Python strings.


1 Answers

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.

like image 75
Ben Bolker Avatar answered Sep 19 '22 15:09

Ben Bolker