Re: [R] blockwise sums

2004-09-01 Thread Vicente Canto Casasola
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) aggreg

RE: [R] blockwise sums

2004-08-31 Thread Berton Gunter
To: 'Barry Rowlingson' > Cc: [EMAIL PROTECTED] > Subject: RE: [R] blockwise sums > > > From: Barry Rowlingson > > > > Liaw, Andy wrote: > > > If you insist, here's one way: > > > > > > my.blockwisesum <- function(x, n, ...) { >

RE: [R] blockwise sums

2004-08-31 Thread Thomas Lumley
On Tue, 31 Aug 2004, Liaw, Andy wrote: > If you insist, here's one way: > > my.blockwisesum <- function(x, n, ...) { > tapply(x, seq(1, length(x), by=n), sum, ...) > } If x is large, rowsum() should be faster than tapply(). -thomas > > Andy > > > From: Lutz Prechelt > > > > I am lo

Re: [R] blockwise sums

2004-08-31 Thread Gabor Grothendieck
Lutz Prechelt 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.blo

Re: [R] blockwise sums

2004-08-31 Thread Peter Dalgaard
Barry Rowlingson <[EMAIL PROTECTED]> writes: > 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

RE: [R] blockwise sums

2004-08-31 Thread Pfaff, Bernhard
> > 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, ...) : >

Re: [R] blockwise sums

2004-08-31 Thread Dimitris Rizopoulos
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 Doct

RE: [R] blockwise sums

2004-08-31 Thread Liaw, Andy
> 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.bl

Re: [R] blockwise sums

2004-08-31 Thread Wolski
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 **

Re: [R] blockwise sums

2004-08-31 Thread 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: > my.blockwisesum(1:10, 3) Error in tapply(x, seq(1, length(x), by = n), sum, ...) : arguments must have same length Here'

RE: [R] blockwise sums

2004-08-31 Thread Liaw, Andy
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