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)
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
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
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
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 ***
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)
-
From: Lutz Prechelt [EMAIL PROTECTED]
To: [EMAIL PROTECTED]
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
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
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(1, length(x), by =
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
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 looking for a
: [R] blockwise sums
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
12 matches
Mail list logo