[R] Why does aggregate fail?

David Winsemius dwinsemius at comcast.net
Sun Feb 7 19:27:56 CET 2010


On Feb 7, 2010, at 1:08 PM, James Rome wrote:

> I am trying to get hourly totals, given 15-minute bins.
> s = seq(0, 95, 1)
> s = floor(s/4)   # 0  0  0  0  1  1  1  1  2  2  2  2  3  3  3  3   
> 4 . . .
>
>> s
> [1]  0  0  0  0  1  1  1  1  2  2  2  2  3  3  3  3  4  4  4  4  5  5
> 5  5  6
> [26]  6  6  6  7  7  7  7  8  8  8  8  9  9  9  9 10 10 10 10 11 11 11
> 11 12 12
> [51] 12 12 13 13 13 13 14 14 14 14 15 15 15 15 16 16 16 16 17 17 17 17
> 18 18 18
> [76] 18 19 19 19 19 20 20 20 20 21 21 21 21 22 22 22 22 23 23 23 23
>
>> mode(d)
> [1] "list"
>> d
>       0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
> 25 26
> Sunday 0 1 0 0 0 0 0 0 0 0  0  0  0  0  0  0  0  2  0  0  0  0  0   
> 0  0
> 0  0
>       27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47  
> 48
> 49 50
> Sunday  0  0  0  0  0  5  5  5  0  0  0  0  0  0  0  6  0  3  1  0  1
> 6  8  9
>       51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71  
> 72
> 73 74
> Sunday  7  9 10  5  0  1  0  1  1  1  0  0  0  1  0  0  7 10  9  9 11
> 11  8  8
>       75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
> Sunday 10  7  6  7  8  7  4  4  6  5  5  5  5  0  0  0  1  6  2  3  0
>> x = aggregate(d, by=list(s), FUN="sum")
> Error in FUN(X[[1L]], ...) : arguments must have same length

I don't know what sort of error is occurring. You have not created a  
posting that easily lets us see what sort of object "d" really is.  
(And it is not being display as though it were a simple list.)

  dput(d) would have allowed us to see what sort of attributes it has.  
Your code works if one strips out the data and puts it into a vector.

 > s = seq(0, 95, 1)
 > s = floor(s/4)

 > d <- scan()
1: 0 1 0 0 0 0 0 0 0 0  0  0  0  0  0  0  0  2  0  0  0  0  0  0  0
26: 0  0
28: 0  0  0  0  0  5  5  5  0  0  0  0  0  0  0  6  0  3  1  0  1
49: 6  8  9
52: 7  9 10  5  0  1  0  1  1  1  0  0  0  1  0  0  7 10  9  9 11
73: 11  8  8
76: 10  7  6  7  8  7  4  4  6  5  5  5  5  0  0  0  1  6  2  3  0
97:
Read 96 items
 > x = aggregate(d, by=list(s), FUN="sum")
 > x
    Group.1  x
1        0  1
2        1  0
3        2  0
4        3  0
5        4  2
6        5  0
7        6  0
8        7  0
9        8 15
10       9  0
11      10  6
12      11  5
13      12 30
14      13 24
15      14  3
16      15  1
17      16  8
18      17 39
19      18 37
20      19 28
21      20 21
22      21 20
23      22  1
24      23 11

>> length(s)
> [1] 96
>> length(d)
> [1] 96
>
> What am I doing wrong?
>
> Thanks in advance list,
> Jim Rome
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

David Winsemius, MD
Heritage Laboratories
West Hartford, CT



More information about the R-help mailing list