[R] Why does aggregate fail?

James Rome jamesrome at gmail.com
Sun Feb 7 20:11:32 CET 2010


This works. But I wish I could write it without a lot of trial and
error. :-(

ha = matrix(nrow=7, ncol=24)
colnames(ha) = as.character(c(0:23))
rownames(ha) = rownames(maxrdf)
m = as.matrix(maxrdf)
for(j in 1:7) {
	x = aggregate(m[j,], by=list(s), FUN=sum)		
	ha[j,] = x[[2]]
}


On 2/7/2010 1:57 PM, James Rome wrote:
On 2/7/2010 1:35 PM, David Winsemius wrote:But to answer your question:

> apply(d, 1, function(z) aggregate(z, by=list(s), FUN=sum) )

David,

That works, but I do not understand why I could not use aggregate
directly. And the answer comes out as a list, which thus far baffles me.
How do I get the answer as a matrix in my original code, which I
modified to use apply?

ha = matrix(nrow=7, ncol=24)
colnames(ha) = as.character(c(0:23))
rownames(ha) = rownames(maxrdf)
for(j in 1:7) {
    x = apply(maxrdf[j,], 1, function(z) aggregate(z, by=list(s),
FUN=sum) )
    ha[j,] = x[[1]][2]
}

Unfortunately, ha gets converted into a list, and then I can't use it
for my plots. And you can probably educate me on how to get what I am
aiming for (a matrix with the rows as the days, the columns as the
hours, and the content as the hourly sum of the 15-minute chunks)
without using the above for loop.

Thanks for the help,
Jim



More information about the R-help mailing list