Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
656 views
in Technique[技术] by (71.8m points)

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

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...