[R] Implementing R's recycling rule

Richard.Cotton at hsl.gov.uk Richard.Cotton at hsl.gov.uk
Tue Oct 19 11:30:35 CEST 2010


> x <- c(1, 2, 3)
> n <- 10
> ## so using the recycling rules, I would like to get from FUN(x, n)==1
> ## I am doing:
> xRecycled <- rep(x, length.out=n)[n]
> 
> This works, but it seems to me that I am missing something really basic 
here
> - is there  more straightforward way of doing this?

x[n %% length(x)] gives you the same answer as rep(x, length.out=n)[n], 
without having to create the longer vector.

Regards,
Richie.

Mathematical Sciences Unit
HSL
4D Pie Charts



------------------------------------------------------------------------
ATTENTION:

This message contains privileged and confidential inform...{{dropped:22}}



More information about the R-help mailing list