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

Popular posts from this blog

jQuery Mobile app not scrolling in Firefox -

c++ - How to add Crypto++ library to Qt project -

php array slice every 2th rule -