[R] Why does aggregate fail?

James Rome jamesrome at gmail.com
Sun Feb 7 19:37:12 CET 2010


On 2/7/2010 1:32 PM, David Winsemius wrote:You have a dataframe with 96
columns and a single row named "Sunday". My guess is that was not your
intent. How did "d" come to exist?

I was trying to make a simpler example. The actual code uses a data
frame maxrdf:
> dput(maxrdf)
structure(list(`0` = c(0, 1, 0, 3, 0, 2, 3), `1` = c(1, 1, 0,
2, 0, 1, 2), `2` = c(0, 1, 1, 1, 0, 2, 1), `3` = c(0, 1, 0, 1,
0, 1, 1), `4` = c(0, 1, 3, 2, 0, 1, 1), `5` = c(0, 0, 0, 1, 0,
1, 1), `6` = c(0, 0, 0, 1, 0, 1, 1), `7` = c(0, 0, 1, 0, 0, 0,
0), `8` = c(0, 1, 1, 0, 0, 2, 1), `9` = c(0, 0, 1, 2, 0, 3, 0
), `10` = c(0, 1, 0, 0, 1, 2, 0), `11` = c(0, 0, 0, 0, 0, 0,
0), `12` = c(0, 0, 0, 0, 0, 0, 1), `13` = c(0, 0, 1, 0, 0, 0,
0), `14` = c(0, 0, 0, 0, 0, 0, 0), `15` = c(0, 0, 0, 1, 0, 0,
0), `16` = c(0, 1, 1, 1, 0, 1, 0), `17` = c(2, 1, 1, 2, 0, 0,
1), `18` = c(0, 1, 3, 4, 0, 4, 2), `19` = c(0, 0, 3, 4, 0, 4,
5), `20` = c(0, 0, 5, 3, 1, 0, 4), `21` = c(0, 0, 5, 5, 2, 0,
4), `22` = c(0, 0, 5, 7, 0, 0, 5), `23` = c(0, 0, 7, 9, 10, 0,
7), `24` = c(0, 0, 6, 8, 4, 5, 9), `25` = c(0, 0, 6, 4, 5, 4,
7), `26` = c(0, 0, 4, 6, 5, 4, 5), `27` = c(0, 0, 7, 9, 8, 5,
10), `28` = c(0, 2, 9, 13, 0, 5, 14), `29` = c(0, 2, 10, 11,
0, 9, 11), `30` = c(0, 3, 9, 8, 0, 8, 9), `31` = c(0, 5, 6, 7,
0, 3, 7), `32` = c(5, 7, 7, 5, 4, 5, 7), `33` = c(5, 6, 8, 5,
7, 6, 5), `34` = c(5, 4, 5, 5, 4, 5, 6), `35` = c(0, 4, 6, 4,
1, 3, 5), `36` = c(0, 6, 5, 5, 4, 7, 5), `37` = c(0, 6, 6, 6,
5, 7, 5), `38` = c(0, 8, 6, 6, 5, 4, 5), `39` = c(0, 6, 5, 3,
3, 4, 4), `40` = c(0, 5, 2, 5, 3, 3, 2), `41` = c(0, 4, 5, 3,
4, 3, 4), `42` = c(6, 5, 6, 0, 3, 2, 5), `43` = c(0, 7, 4, 0,
3, 2, 6), `44` = c(3, 7, 6, 6, 5, 8, 4), `45` = c(1, 8, 5, 3,
2, 5, 9), `46` = c(0, 8, 7, 7, 0, 6, 5), `47` = c(1, 8, 5, 4,
0, 8, 8), `48` = c(6, 5, 8, 0, 0, 4, 0), `49` = c(8, 6, 13, 7,
0, 8, 0), `50` = c(9, 7, 8, 7, 0, 7, 0), `51` = c(7, 7, 8, 10,
0, 6, 0), `52` = c(9, 1, 8, 0, 4, 5, 0), `53` = c(10, 0, 1, 0,
1, 0, 1), `54` = c(5, 0, 3, 0, 3, 0, 0), `55` = c(0, 0, 1, 1,
5, 0, 0), `56` = c(1, 0, 10, 5, 10, 1, 0), `57` = c(0, 0, 8,
6, 12, 0, 1), `58` = c(1, 0, 8, 4, 11, 0, 0), `59` = c(1, 0,
6, 5, 6, 0, 0), `60` = c(1, 0, 0, 1, 4, 0, 0), `61` = c(0, 0,
0, 4, 9, 0, 1), `62` = c(0, 0, 0, 2, 5, 0, 0), `63` = c(0, 0,
0, 5, 12, 0, 0), `64` = c(1, 0, 0, 0, 9, 1, 1), `65` = c(0, 0,
0, 1, 7, 0, 0), `66` = c(0, 6, 8, 3, 6, 3, 4), `67` = c(7, 7,
7, 3, 9, 5, 6), `68` = c(10, 6, 7, 0, 6, 6, 6), `69` = c(9, 10,
9, 5, 9, 7, 8), `70` = c(9, 9, 10, 6, 8, 7, 8), `71` = c(11,
10, 11, 7, 11, 8, 8), `72` = c(11, 0, 9, 6, 10, 7, 7), `73` = c(8,
0, 9, 7, 8, 9, 12), `74` = c(8, 3, 9, 0, 9, 9, 7), `75` = c(10,
4, 10, 0, 10, 7, 6), `76` = c(7, 0, 8, 8, 9, 9, 11), `77` = c(6,
0, 12, 8, 9, 10, 7), `78` = c(7, 0, 9, 5, 9, 9, 7), `79` = c(8,
0, 8, 7, 8, 8, 7), `80` = c(7, 11, 7, 8, 4, 8, 5), `81` = c(4,
6, 9, 5, 7, 7, 1), `82` = c(4, 6, 5, 10, 9, 10, 1), `83` = c(6,
5, 6, 7, 9, 7, 0), `84` = c(5, 0, 8, 3, 8, 8, 0), `85` = c(5,
1, 5, 6, 8, 7, 0), `86` = c(5, 2, 5, 3, 7, 7, 0), `87` = c(5,
3, 5, 6, 8, 7, 0), `88` = c(0, 4, 6, 0, 6, 6, 0), `89` = c(0,
1, 8, 0, 4, 7, 0), `90` = c(0, 0, 5, 0, 7, 2, 0), `91` = c(1,
0, 3, 0, 2, 5, 0), `92` = c(6, 0, 5, 0, 6, 5, 0), `93` = c(2,
0, 4, 0, 5, 5, 0), `94` = c(3, 0, 3, 0, 2, 6, 0), `95` = c(0,
0, 3, 0, 4, 4, 0)), .Names = c("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", "27",
"28", "29", "30", "31", "32", "33", "34", "35", "36", "37", "38",
"39", "40", "41", "42", "43", "44", "45", "46", "47", "48", "49",
"50", "51", "52", "53", "54", "55", "56", "57", "58", "59", "60",
"61", "62", "63", "64", "65", "66", "67", "68", "69", "70", "71",
"72", "73", "74", "75", "76", "77", "78", "79", "80", "81", "82",
"83", "84", "85", "86", "87", "88", "89", "90", "91", "92", "93",
"94", "95"), row.names = c("Sunday", "Monday", "Tuesday", "Wednesday",
"Thursday", "Friday", "Saturday"), class = "data.frame")
> > maxrdf[1,]
       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
>

And the code that fails is

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



More information about the R-help mailing list