[R] Implementing R's recycling rule

Erich Neuwirth erich.neuwirth at univie.ac.at
Tue Oct 19 12:10:49 CEST 2010



On 10/19/2010 11:47 AM, Rainer M Krug wrote:
>> x[n %% length(x)] gives you the same answer as rep(x, length.out=n)[n],
>> without having to create the longer vector.
>>


n %% length(x) may return 0 and in that case,
x[n %% length(x)] will not give the result you expect.

x[((n - 1)  %% length(x)) + 1]
might be what you want.



More information about the R-help mailing list