[R] Can this loop be delooped?

Talbot Katz topkatz at msn.com
Fri Feb 2 17:13:33 CET 2007


Jim and Heikki,

Thank you so much for your inventive solutions, much better and more 
efficient than my approach!  As a footnote to my original post, it turns out 
that using lapply rather than sapply consistently returns a list, and not an 
array (which sapply gives when the number of points is an exact multiple of 
the number of groups).

--  TMK  --
212-460-5430	home
917-656-5351	cell



>From: "Kaskelma, Heikki" <kashei at sip-oy.com>
>To: "jim holtman" <jholtman at gmail.com>,"Talbot Katz" <topkatz at msn.com>
>CC: <r-help at stat.math.ethz.ch>
>Subject: RE: [R] Can this loop be delooped?
>Date: Fri, 2 Feb 2007 16:06:51 +0200
>
>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