Reputation: 7545
a <- data.frame(1:50)
a %% 10==0
It returns TRUE for row numbers in increments of 10, starting at 0.
a %% 10==2
This does the same thing, but starting at 2.
How does this syntax work? I know what %% is as an operator but I don't understand what's happening in this scenario.
Upvotes: 2
Views: 459
Reputation: 2979
(Hope I haven't over explained this and interpreted what you're asking correctly:)
As you've said, you know that %%
is the modulus operator. So
a <- data.frame(1:50)
a %% 10
returns 'x mod 10' for each item in the frame (or 1,2,3,4,5,6,7,8,9,0 etc etc)
The two examples you've given are like 'shorthand' for if
statements. In pseudo code:
for n = 1 to length of a
if a[n] %% 10 equals zero
return true
else
return false
So a %% 10 == 0
is "Does the element mod 10 equal zero" and a %% 10 == 2
is "Does the element mod 10 equal two" applied to each element
And the result is matrix of logicals (booleans).
Upvotes: 2