[R] Why does aggregate fail?

David Winsemius dwinsemius at comcast.net
Sun Feb 7 20:23:15 CET 2010


On Feb 7, 2010, at 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.

It comes out as a data.frame, ... just as promised in the help page.

> How do I get the answer as a matrix in my original code, which I
> modified to use apply?

You could coerce either maxrdf or the aggregate returns to a matrix  
with as.matrix or data.matrix.

>
> 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.

apply( data.matrix(maxrdf), 1  # loops over the rows
       function(z) aggregate(z, by=s, sum)
       )
#Gives you a bunch of dataframes produced by the serial application of  
aggregate.

sapply(apply(maxrdf, 1, function(z) aggregate(z, by=list(s), sum) ),  
'[', 2)

#Gives you a list of vectors by day...almost what you wanted ...
#  the  ' "[", 2' part is the extraction of the second column of the  
dataframe

# And what I think you were asking for:

do.call(rbind,
    sapply(apply(maxrdf, 1,
       function(z) aggregate(z, by=list(s), sum) ), '[', 2) )

# ... as a matrix with named rows
             [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11]  
[,12] [,13] [,14] [,15] [,16] [,17]
Sunday.x       1    0    0    0    2    0    0    0   15     0      
6     5    30    24     3     1     8
Monday.x       4    1    2    0    3    0    0   12   21    26     
21    31    25     1     0     0    13
Tuesday.x      1    4    2    1    8   22   23   34   26    22     
17    23    37    13    32     0    15
Wednesday.x    7    4    2    1   11   24   27   39   19    20      
8    20    24     1    20    12     7
Thursday.x     0    0    1    0    0   13   22    0   16    17     
13     7     0    13    39    30    31
Friday.x       6    3    7    0    9    0   18   25   19    22     
10    27    25     5     1     0     9
Saturday.x     7    3    1    1    8   20   31   41   23    19     
17    26     0     1     1     1    11
             [,18] [,19] [,20] [,21] [,22] [,23] [,24]
Sunday.x       39    37    28    21    20     1    11
Monday.x       35     7     0    28     6     5     0
Tuesday.x      37    37    37    27    23    22    15
Wednesday.x    18    13    28    30    18     0     0
Thursday.x     34    37    35    29    31    19    17
Friday.x       28    32    36    32    29    20    20
Saturday.x     30    32    32     7     0     0     0


>
> Thanks for the help,
> Jim

David Winsemius, MD
Heritage Laboratories
West Hartford, CT



More information about the R-help mailing list