I am looking for a function like my.blockwisesum(vector, n) that computes sums of disjoint subsequences of length n from vector and can work with vector lengths that are not a multiple of n. It should give me for instance my.blockwisesum(1:10, 3) == c(6, 15, 24, 10) Is there a builtin function that can do this? One could do it by coercing the vector into a matrix of width n, and then use apply, but that is cumbersome if the length is not divisible by n, is it not? Any other ideas? Lutz Prof. Dr. Lutz Prechelt; prechelt at inf.fu-berlin.de Institut fuer Informatik; Freie Universitaet Berlin Takustr. 9; 14195 Berlin; Germany +49 30 838 75115; http://www.inf.fu-berlin.de/inst/ag-se/
If you insist, here's one way: my.blockwisesum <- function(x, n, ...) { tapply(x, seq(1, length(x), by=n), sum, ...) } Andy> From: Lutz Prechelt > > I am looking for a function like > my.blockwisesum(vector, n) > that computes sums of disjoint subsequences of length n from vector > and can work with vector lengths that are not a multiple of n. > > It should give me for instance > my.blockwisesum(1:10, 3) == c(6, 15, 24, 10) > > Is there a builtin function that can do this? > One could do it by coercing the vector into a matrix of width n, > and then use apply, > but that is cumbersome if the length is not divisible by n, > is it not? > Any other ideas? > > Lutz > > Prof. Dr. Lutz Prechelt; prechelt at inf.fu-berlin.de > Institut fuer Informatik; Freie Universitaet Berlin > Takustr. 9; 14195 Berlin; Germany > +49 30 838 75115; http://www.inf.fu-berlin.de/inst/ag-se/ > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.html > >
Hi! ind<-c(sort(rep(1:floor(length(x)/ 3 ) , 3 )) , floor(length(x)/ 3 )+1) by(x,ind,sum) my.blockwisesum<-function(x,n,...) { ind<-c(sort(rep(1:floor(length(x)/ n ) , n )) , floor(length(x)/ n )+1) return(tapply(x,ind,sum)) } /Eryk *********** REPLY SEPARATOR *********** On 8/31/2004 at 2:19 PM Lutz Prechelt wrote:>>>I am looking for a function like >>> my.blockwisesum(vector, n) >>>that computes sums of disjoint subsequences of length n from vector >>>and can work with vector lengths that are not a multiple of n. >>> >>>It should give me for instance >>> my.blockwisesum(1:10, 3) == c(6, 15, 24, 10) >>> >>>Is there a builtin function that can do this? >>>One could do it by coercing the vector into a matrix of width n, >>>and then use apply, >>>but that is cumbersome if the length is not divisible by n, >>>is it not? >>>Any other ideas? >>> >>> Lutz >>> >>>Prof. Dr. Lutz Prechelt; prechelt at inf.fu-berlin.de >>>Institut fuer Informatik; Freie Universitaet Berlin >>>Takustr. 9; 14195 Berlin; Germany >>>+49 30 838 75115; http://www.inf.fu-berlin.de/inst/ag-se/ >>> >>>______________________________________________ >>>R-help at stat.math.ethz.ch mailing list >>>https://stat.ethz.ch/mailman/listinfo/r-help >>>PLEASE do read the posting guide! http://www.R-project.org/posting-guide.htmlDipl. bio-chem. Witold Eryk Wolski @ MPI-Moleculare Genetic Ihnestrasse 63-73 14195 Berlin 'v' tel: 0049-30-83875219 / \ mail: witek96 at users.sourceforge.net ---W-W---- http://www.molgen.mpg.de/~wolski wolski at molgen.mpg.de
> From: Barry Rowlingson > > Liaw, Andy wrote: > > If you insist, here's one way: > > > > my.blockwisesum <- function(x, n, ...) { > > tapply(x, seq(1, length(x), by=n), sum, ...) > > } > > > > Did you test that? I get:Of course not (slap on wrist)!! My apologies... Andy> > my.blockwisesum(1:10, 3) > Error in tapply(x, seq(1, length(x), by = n), sum, ...) : > arguments must have same length > > > Here's my solution with tapply and rep() to generate a vector like > c(1,1,1,2,2,2,3,3,3,4): > > baz.blockwisesum> > function(v,n){tapply(v,rep(1:(1+length(v)/n),each=n)[1:length( > v)],sum)} > > > baz.blockwisesum(1:10,3) > 1 2 3 4 > 6 15 24 10 > > - just ignore the 1 to 4 names, they cant hurt you. > > Baz > >
Hi Lutz, you could try the following: blockwisesum <- function(x, n){ nx <- length(x) if(nx%%n) x. <- c(x, rep(0., n*ceiling(nx/n)-nx)) else x. <- x x. <- matrix(x., ncol=n, byrow=TRUE) rowSums(x.) } blockwisesum(1:10, 3) I hope this helps. Best, Dimitris ---- Dimitris Rizopoulos Doctoral Student Biostatistical Centre School of Public Health Catholic University of Leuven Address: Kapucijnenvoer 35, Leuven, Belgium Tel: +32/16/396887 Fax: +32/16/337015 Web: http://www.med.kuleuven.ac.be/biostat/ http://www.student.kuleuven.ac.be/~m0390867/dimitris.htm ----- Original Message ----- From: "Lutz Prechelt" <prechelt at pcpool.mi.fu-berlin.de> To: <r-help at stat.math.ethz.ch> Sent: Tuesday, August 31, 2004 2:19 PM Subject: [R] blockwise sums> I am looking for a function like > my.blockwisesum(vector, n) > that computes sums of disjoint subsequences of length n from vector > and can work with vector lengths that are not a multiple of n. > > It should give me for instance > my.blockwisesum(1:10, 3) == c(6, 15, 24, 10) > > Is there a builtin function that can do this? > One could do it by coercing the vector into a matrix of width n, > and then use apply, > but that is cumbersome if the length is not divisible by n, > is it not? > Any other ideas? > > Lutz > > Prof. Dr. Lutz Prechelt; prechelt at inf.fu-berlin.de > Institut fuer Informatik; Freie Universitaet Berlin > Takustr. 9; 14195 Berlin; Germany > +49 30 838 75115; http://www.inf.fu-berlin.de/inst/ag-se/ > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide!http://www.R-project.org/posting-guide.html
> > Liaw, Andy wrote: > > If you insist, here's one way: > > > > my.blockwisesum <- function(x, n, ...) { > > tapply(x, seq(1, length(x), by=n), sum, ...) > > } > > > > Did you test that? I get: > > > my.blockwisesum(1:10, 3) > Error in tapply(x, seq(1, length(x), by = n), sum, ...) : > arguments must have same length > > > Here's my solution with tapply and rep() to generate a vector like > c(1,1,1,2,2,2,3,3,3,4): > > baz.blockwisesum> > function(v,n){tapply(v,rep(1:(1+length(v)/n),each=n)[1:length( > v)],sum)} > > > baz.blockwisesum(1:10,3) > 1 2 3 4 > 6 15 24 10 > > - just ignore the 1 to 4 names, they cant hurt you. > > BazTo complete the picture: here is another one: my.blockwisesum <- function(vec, n){ vec <- as.vector(vec) n <- as.integer(n) total <- length(vec) if(total <= n){ stop("\nn should be smaller than length of vector.\n") } start <- seq(1, total, n) end <- start + n - 1 end[end > total] <- max(start) index <- 1 : length(start) return(sapply(index, function(x)sum(test[start[x]:end[x]]))) }> test <- 1:150 > ptn <- proc.time() > baz.blockwisesum(test,3)1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 6 15 24 33 42 51 60 69 78 87 96 105 114 123 132 141 150 159 168 177 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 186 195 204 213 222 231 240 249 258 267 276 285 294 303 312 321 330 339 348 357 41 42 43 44 45 46 47 48 49 50 366 375 384 393 402 411 420 429 438 447> proc.time()-ptn[1] 0.00 0.00 0.22 NA NA> > ptn <- proc.time() > my.blockwisesum(test,3)[1] 6 15 24 33 42 51 60 69 78 87 96 105 114 123 132 141 150 159 168 [20] 177 186 195 204 213 222 231 240 249 258 267 276 285 294 303 312 321 330 339 [39] 348 357 366 375 384 393 402 411 420 429 438 447> proc.time()-ptn[1] 0.00 0.00 0.19 NA NA>HTH, Bernhard -------------------------------------------------------------------------------- The information contained herein is confidential and is inte...{{dropped}}
Lutz Prechelt <prechelt <at> pcpool.mi.fu-berlin.de> writes: : : I am looking for a function like : my.blockwisesum(vector, n) : that computes sums of disjoint subsequences of length n from vector : and can work with vector lengths that are not a multiple of n. : : It should give me for instance : my.blockwisesum(1:10, 3) == c(6, 15, 24, 10) tapply(v, (seq(v)-1)%/%n, sum)
Hi, all!! From the help page for 'aggregate': Splits the data into subsets, computes summary statistics for each, and returns the result in a convenient form. So here's the solution I found to this problem: blocksums <- function(x,n) { temp <- 1:length(x)-1 temp <- list((temp%/%n)+1) aggregate(x,by=temp,sum)$x } For instance: blocksums(1:10,3) [1] 6 15 24 10 Hope this helps!! Vicente.