r - Wrap around subtraction -
i have these numbers:
login.day$wday [1] 5 6 7 1 2 3 4
and want map them to:
login.day$wday [1] 4 5 6 7 1 2 3
each number subtracted 1, , if answer 0, wrap around 7. embarrassingly simple can't figure out. attempt keeps giving me zero:
> (login.day$wday + 6) %% 7 [1] 4 5 6 0 1 2 3
prefer solution in r. possible modulo arithmetic or must use if statement
?
mathematically equivalent other solution, , explanation.
(login.day$wday - 1 - 1) %% 7 + 1
the problem is hard modular arithmetic numbers starting @ 1
.
we start doing -1
shift down 1
, have zero-based numbers ranging [0,6]
.
we subtract 1
, because trying begin with.
next, take modulus, , add 1
shift range [1,7]
.
Comments
Post a Comment