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" <[EMAIL PROTECTED]> >To: "jim holtman" <[EMAIL PROTECTED]>,"Talbot Katz" <[EMAIL PROTECTED]> >CC: <[email protected]> >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 <[EMAIL PROTECTED]> 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)) ______________________________________________ [email protected] mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
