Hi Stephen,
how about this:
chop<-function(x,counts){
stopifnot(sum(counts)==length(x))
split(9:1,unlist(mapply(rep,seq_along(counts),counts)))
}
chop(9:1,c(3,2,4))
cheers
Am 02.08.2012 12:29, schrieb Stephen Eglen:> Anyone got a neat way to chop a vector up into smaller subvectors?
> This is what I have now, which seems inelegant:
>
> chop <- function(v, counts) {
> stopifnot(sum(counts)==length(v))
> end <- cumsum(counts)
> beg <- c(1, 1+end[-length(end)])
> begend <- cbind(beg, end)
> apply(begend, 1, function(x) v[x[1]:x[2]])
> }
>
>
>> chop(9:1, c(3,2,4))
> [[1]]
> [1] 9 8 7
>
> [[2]]
> [1] 6 5
>
> [[3]]
> [1] 4 3 2 1
>
> ______________________________________________
> R-help at r-project.org 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.
>
--
Eik Vettorazzi
Department of Medical Biometry and Epidemiology
University Medical Center Hamburg-Eppendorf
Martinistr. 52
20246 Hamburg
T ++49/40/7410-58243
F ++49/40/7410-57790
--
Pflichtangaben gem?? Gesetz ?ber elektronische Handelsregister und
Genossenschaftsregister sowie das Unternehmensregister (EHUG):
Universit?tsklinikum Hamburg-Eppendorf; K?rperschaft des ?ffentlichen Rechts;
Gerichtsstand: Hamburg
Vorstandsmitglieder: Prof. Dr. Guido Sauter (Vertreter des Vorsitzenden), Dr.
Alexander Kirstein, Joachim Pr?l?, Prof. Dr. Dr. Uwe Koch-Gromus