[R] Can this loop be delooped?

Kaskelma, Heikki kashei at sip-oy.com
Fri Feb 2 15:06:51 CET 2007


Consider

na=43; nb=5; x=1:na
ns=rep(na %/% nb, nb) + (1:nb <= na %% nb)
split(x, rep(1:nb, ns))


Heikki Kaskelma

On Fri, 2 Feb 2007, jim holtman <jholtman at gmail.com> wrote:
>This might do what you want:
>
> # test data
> x <- 1:43
> nb <- 5  # number of subsets
> # create vector of lengths of subsets
> ns <- rep(length(x) %/% nb, nb)
> # see if we have to adjust counts of initial subsets
> if ((.offset <- length(x) %% nb) != 0) ns[1:.offset] = ns[1:.offset] +
1
> # create the subsets
> split(x, rep(1:nb,ns))



More information about the R-help mailing list