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
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, ...) {
>
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
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
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
>
> 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, ...) :
>
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
> 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
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 **
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'
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
11 matches
Mail list logo