problem with plyr -ddply

6 views
Skip to first unread message

simeond

unread,
Mar 24, 2009, 5:36:20 AM3/24/09
to ggplot2
Folks

I am having some problems with ddply. I want to be able to split up a
dataframe, convert it to zoo, then aggregate the data from weekly to
monthly and stick it together as another data frame.

I can get it to write to a list in the way I want (using dlply) but
not to a dataframe. The error message says ... "Error in
list_to_dataframe(res, attr(.data, "split_labels")) : Results are not
equal lengths". But they both seem to me to be the same size (ie 7).

Any help much appreciated.

simeon

data.frame': 312 obs. of 5 variables:
$ Date :Class 'Date' num [1:312] 13970 14000 14031 14061 14092 ...
$ xx : num 152 164 277 189 153 ...
$ yy : num 325104 357972 505634 302559 159574 ...
$ Media: Factor w/ 7 levels "Affiliates","Display",..: 3 3 3 3 3 3 3
3 3 3 ...
$ zz : num 2137 2188 1825 1605 1042 ...


to.monthly <- function (df) {
df.z <- zoo(df[c("yy","zz")],df$Date)
df.zz<- aggregate(df.z,as.yearmon,sum,na.rm=T)
df.zz$CPA <- with(df.zz,ifelse(zz==0,0,yy/zz))
return(df.zz)
}

df.month <- ddply(weekly.df,.(Media),to.monthly)
df.month.l <- dlply(weekly.df,.(Media),to.monthly)


hadley wickham

unread,
Mar 24, 2009, 9:41:59 AM3/24/09
to simeond, ggplot2
Hi Simeon,

Could you provide a reproducible example please? It's hard to figure
out what the problem is without the original data.

Hadley
--
http://had.co.nz/

simeon duckworth

unread,
Mar 24, 2009, 4:27:04 PM3/24/09
to hadley wickham, ggplot2
hadley

sorry.  here is the code & the full data set

thanks for any help

simeon


to.monthly <- function (df) {
  df.z <- zoo(df[c("yy","zz")],df$Date)
   df.zz<- aggregate(df.z,as.yearmon,sum,
na.rm=T)
   df.zz$CPA <- with(df.zz,ifelse(zz==0,0,yy/zz))
   return(df.zz)
   }

  df.month   <- ddply(weekly.df,.(Media),to.monthly)
 df.month.l <- dlply(weekly.df,.(Media),to.monthly)



         Date        xx        yy      Media          zz
1   2008-04-01  152.1500 325104.00         DM 2136.733487
2   2008-05-01  163.5800 357972.00         DM 2188.360435
3   2008-06-01  277.0600 505634.00         DM 1824.998195
4   2008-07-01  188.5100 302559.00         DM 1605.002387
5   2008-08-01  153.1900 159574.00         DM 1041.673738
6   2008-09-01  149.2300 436498.00         DM 2925.001675
7   2008-10-01  118.9700 256767.00         DM 2158.249979
8   2008-11-01   83.3700 366131.00         DM 4391.639679
9   2008-12-01  105.1400 127565.00         DM 1213.287046
10  2009-01-01   77.3200 580798.00         DM 7511.614071
11  2009-02-01  119.3200 306261.00         DM 2566.719745
12  2008-03-30   31.9000  16908.00 Affiliates  530.031348
13  2008-04-06   31.6500  18168.00 Affiliates  574.028436
14  2008-04-13   35.1800  16463.00 Affiliates  467.964753
15  2008-04-20   28.4900  12451.00 Affiliates  437.030537
16  2008-04-27   31.9300  12805.00 Affiliates  401.033511
17  2008-05-04   40.7200  16453.00 Affiliates  404.052063
18  2008-05-11   43.0700  14128.00 Affiliates  328.024147
19  2008-05-18   46.2800  14949.00 Affiliates  323.012100
20  2008-05-25   44.0400  18937.00 Affiliates  429.995459
21  2008-06-01   43.7600  18685.00 Affiliates  426.988117
22  2008-06-08   45.5700  26110.00 Affiliates  572.964670
23  2008-06-15   51.2400  26337.00 Affiliates  513.992974
24  2008-06-22   48.4100  29963.00 Affiliates  618.942367
25  2008-06-29   51.7400  33424.00 Affiliates  645.999227
26  2008-07-06   51.5300  31022.00 Affiliates  602.018242
27  2008-07-13   54.4300  30101.00 Affiliates  553.022230
28  2008-07-20   74.1100  47578.00 Affiliates  641.991634
29  2008-07-27   95.2300 104465.00 Affiliates 1096.975743
30  2008-08-03   55.5000  33743.00 Affiliates  607.981982
31  2008-08-10   55.6800  31181.00 Affiliates  560.003592
32  2008-08-17   58.1500  37796.00 Affiliates  649.974205
33  2008-08-24   55.2400  32372.00 Affiliates  586.024620
34  2008-08-31   54.0900  33966.00 Affiliates  627.953411
35  2008-09-07   56.6100  32944.00 Affiliates  581.946653
36  2008-09-14   63.6900  30318.00 Affiliates  476.024494
37  2008-09-21   63.9500  26282.00 Affiliates  410.977326
38  2008-09-28   65.1300  34322.00 Affiliates  526.976816
39  2008-10-05   59.1000  31798.00 Affiliates  538.037225
40  2008-10-12   58.0600  30017.00 Affiliates  516.999656
41  2008-10-19   58.8200  29587.00 Affiliates  503.009181
42  2008-10-26   57.3200  32327.00 Affiliates  563.974180
43  2008-11-02   56.4900  32763.00 Affiliates  579.978757
44  2008-11-09   59.1300  41629.00 Affiliates  704.025030
45  2008-11-16   57.4500  37401.00 Affiliates  651.018277
46  2008-11-23   54.9800  29359.00 Affiliates  533.994180
47  2008-11-30   57.8000  26183.00 Affiliates  452.993080
48  2008-12-07   62.8500  20300.00 Affiliates  322.991249
49  2008-12-14   64.9000  19274.00 Affiliates  296.979969
50  2008-12-21   73.8600  13886.00 Affiliates  188.004333
51  2008-12-28   56.2100  27151.00 Affiliates  483.027931
52  2009-01-04   55.9400  33282.00 Affiliates  594.958885
53  2009-01-11   56.3000  33440.00 Affiliates  593.960924
54  2009-01-18   54.9300  31530.00 Affiliates  574.003277
55  2009-01-25   59.7000  32535.00 Affiliates  544.974874
56  2009-02-01   60.3900  31765.00 Affiliates  525.997682
57  2009-02-08   61.3300  29620.00 Affiliates  482.961030
58  2009-02-15   62.9200  27309.00 Affiliates  434.027336
59  2009-02-22   61.2400  27009.00 Affiliates  441.035271
60  2009-03-01   61.8600  27031.00 Affiliates  436.970579
61  2009-03-08   59.4400  25442.00 Affiliates  428.028264
62  2008-03-30   70.6100  11368.00    Display  160.997026
63  2008-04-06   23.3700   2221.00    Display   95.036371
64  2008-04-13  168.6100  20571.00    Display  122.003440
65  2008-04-20  308.9900  42022.00    Display  135.997929
66  2008-04-27  298.6100  65695.00    Display  220.002679
67  2008-05-04   70.8600  11834.00    Display  167.005363
68  2008-05-11  106.8700  14748.00    Display  137.999439
69  2008-05-18  174.0400  30284.00    Display  174.005976
70  2008-05-25  211.7700  38330.00    Display  180.998253
71  2008-06-01  169.8800  36354.00    Display  213.998116
72  2008-06-08   69.4800  21955.00    Display  315.990213
73  2008-06-15   81.2400  15517.00    Display  191.001969
74  2008-06-22   94.7200  20364.00    Display  214.991554
75  2008-06-29   76.8500  16138.00    Display  209.993494
76  2008-07-06   75.0400  15909.00    Display  212.006930
77  2008-07-13   81.3100  16181.00    Display  199.003813
78  2008-07-20   86.7000  16386.00    Display  188.996540
79  2008-07-27   83.3300  16249.00    Display  194.995800
80  2008-08-03   75.4700  16303.00    Display  216.019610
81  2008-08-10   79.3400  15869.00    Display  200.012604
82  2008-08-17   68.0600  18376.00    Display  269.997061
83  2008-08-24   73.3400  15328.00    Display  208.999182
84  2008-08-31   73.9800  18939.00    Display  256.001622
85  2008-09-07   65.3300  15158.00    Display  232.022042
86  2008-09-14   56.2800  16942.00    Display  301.030561
87  2008-09-21   84.4600  19509.00    Display  230.985082
88  2008-09-28   39.6600  10272.00    Display  259.001513
89  2008-10-05   73.8700  22972.00    Display  310.978746
90  2008-10-12  107.6300  27232.00    Display  253.014959
91  2008-10-19  122.4600  24491.00    Display  199.991834
92  2008-10-26   90.9600  18466.00    Display  203.012313
93  2008-11-02   82.9800  15767.00    Display  190.009641
94  2008-11-09   77.7700  19366.00    Display  249.016330
95  2008-11-16   95.0400  16631.00    Display  174.989478
96  2008-11-23   86.1600  11805.00    Display  137.012535
97  2008-11-30   81.5400   9540.00    Display  116.997792
98  2008-12-07   84.9100   9340.00    Display  109.998822
99  2008-12-14   93.1100   9125.00    Display   98.002363
100 2008-12-21  111.9500   9404.00    Display   84.001787
101 2008-12-28   79.0100  11377.00    Display  143.994431
102 2009-01-04   77.8400  13855.00    Display  177.993320
103 2009-01-11   92.4400  12757.00    Display  138.003029
104 2009-01-18  115.7700  17829.00    Display  154.003628
105 2009-01-25  110.6400  21575.00    Display  195.001808
106 2009-02-01   84.2300  13140.00    Display  156.001425
107 2009-02-08  115.5800  13408.00    Display  116.006229
108 2009-02-15  122.1400  17955.00    Display  147.003439
109 2009-02-22  107.1300  21105.00    Display  197.003640
110 2009-03-01  105.2400  16208.00    Display  154.009882
111 2009-03-08  195.4700  38507.00    Display  196.996982
112 2008-03-30   35.2800  32280.00   Listings  914.965986
113 2008-04-06   38.5500  14609.00   Listings  378.962387
114 2008-04-13   44.4600  10004.00   Listings  225.011246
115 2008-04-20   44.8700   6730.00   Listings  149.988857
116 2008-04-27   48.0500  15713.00   Listings  327.013528
117 2008-05-04   53.0000  14627.00   Listings  275.981132
118 2008-05-11   50.3800  16778.00   Listings  333.028980
119 2008-05-18   49.6700  13958.00   Listings  281.014697
120 2008-05-25   47.2100  11188.00   Listings  236.983690
121 2008-06-01   59.2100  13558.00   Listings  228.981591
122 2008-06-08   67.6200  32524.00   Listings  480.981958
123 2008-06-15   64.9000  30309.00   Listings  467.010786
124 2008-06-22   51.3700  16490.00   Listings  321.004477
125 2008-06-29   56.3900  13759.00   Listings  243.997163
126 2008-07-06   58.8700  18426.00   Listings  312.994734
127 2008-07-13   59.6200  14129.00   Listings  236.984233
128 2008-07-20   63.3100  11713.00   Listings  185.010267
129 2008-07-27   61.0300  11839.00   Listings  193.986564
130 2008-08-03   77.5700  15359.00   Listings  198.001805
131 2008-08-10   79.9900  13039.00   Listings  163.007876
132 2008-08-17   70.3000  11388.00   Listings  161.991465
133 2008-08-24   76.1400  13097.00   Listings  172.012083
134 2008-08-31   79.5500  17979.00   Listings  226.008799
135 2008-09-07   72.2500  17556.00   Listings  242.989619
136 2008-09-14   73.0200  16868.00   Listings  231.005204
137 2008-09-21   76.2200  13795.00   Listings  180.989242
138 2008-09-28   60.0500  11169.00   Listings  185.995004
139 2008-10-05   36.9800   6620.00   Listings  179.015684
140 2008-10-12   44.3900   6215.00   Listings  140.009011
141 2008-10-19   45.0700   6625.00   Listings  146.993566
142 2008-10-26   44.4600   5513.00   Listings  123.999100
143 2008-11-02   45.7900   5311.00   Listings  115.986023
144 2008-11-09   44.3600   5367.00   Listings  120.987376
145 2008-11-16   44.9900   4589.00   Listings  102.000445
146 2008-11-23   49.0600   3679.00   Listings   74.989808
147 2008-11-30   43.7300   3149.00   Listings   72.010062
148 2008-12-07   54.2600   3527.00   Listings   65.001843
149 2008-12-14   58.9600   3007.00   Listings   51.000678
150 2008-12-21   61.3000   2145.00   Listings   34.991843
151 2008-12-28   58.5000   4563.00   Listings   78.000000
152 2009-01-04   69.9200  11606.00   Listings  165.989703
153 2009-01-11  100.9900  11714.00   Listings  115.991682
154 2009-01-18  132.8900  14618.00   Listings  110.000753
155 2009-01-25  101.4900  11975.00   Listings  117.991920
156 2009-02-01   58.3700   7764.00   Listings  133.013534
157 2009-02-08   57.1300   5713.00   Listings  100.000000
158 2009-02-15   62.4600   7057.00   Listings  112.984310
159 2009-02-22   61.2100   5754.00   Listings   94.004248
160 2009-03-01   71.9900   6119.00   Listings   84.997916
161 2009-03-08   59.7100   5195.00   Listings   87.003852
162 2008-03-30   34.4900  26037.00        PPC  754.914468
163 2008-04-06   28.5100  22184.00        PPC  778.112943
164 2008-04-13   32.0200  23249.00        PPC  726.077452
165 2008-04-20   45.7700  22654.00        PPC  494.953026
166 2008-04-27   70.9800  29101.00        PPC  409.988729
167 2008-05-04   41.5400  19858.00        PPC  478.045258
168 2008-05-11   48.9900  26110.00        PPC  532.965911
169 2008-05-18   36.6000  20971.00        PPC  572.978142
170 2008-05-25   68.8800  45458.00        PPC  659.959350
171 2008-06-01   73.7700  43157.00        PPC  585.021011
172 2008-06-08   63.1000  47580.00        PPC  754.041204
173 2008-06-15   76.1400  53221.00        PPC  698.988705
174 2008-06-22   76.4000  58293.00        PPC  762.997382
175 2008-06-29   75.5800  50862.00        PPC  672.955808
176 2008-07-06   84.0200  57805.00        PPC  687.990955
177 2008-07-13   86.7800  59269.00        PPC  682.979949
178 2008-07-20   73.0400  51055.00        PPC  699.000548
179 2008-07-27   60.3200  41196.00        PPC  682.957560
180 2008-08-03   60.0400  37948.00        PPC  632.045303
181 2008-08-10   48.9500  30787.00        PPC  628.947906
182 2008-08-17   43.6200  26784.00        PPC  614.030261
183 2008-08-24   29.9800  16401.00        PPC  547.064710
184 2008-08-31   21.2100  14042.00        PPC  662.046205
185 2008-09-07   24.6300  18742.00        PPC  760.941941
186 2008-09-14   31.9700  22855.00        PPC  714.888958
187 2008-09-21   58.3500  42419.00        PPC  726.975150
188 2008-09-28   57.7900  36638.00        PPC  633.985119
189 2008-10-05   42.7700  31266.00        PPC  731.026420
190 2008-10-12   49.9600  34275.00        PPC  686.048839
191 2008-10-19   52.0900  39379.00        PPC  755.980035
192 2008-10-26   52.6000  36659.00        PPC  696.939163
193 2008-11-02   37.5700  21641.00        PPC  576.018100
194 2008-11-09   13.3500   9254.00        PPC  693.183521
195 2008-11-16   20.9200  12823.00        PPC  612.954111
196 2008-11-23   26.4200  17517.00        PPC  663.020439
197 2008-11-30   26.4400  13090.00        PPC  495.083207
198 2008-12-07   29.6900  15943.00        PPC  536.982149
199 2008-12-14   38.8700  17684.00        PPC  454.952405
200 2008-12-21   49.0300  13827.00        PPC  282.011014
201 2008-12-28   35.6100  24075.00        PPC  676.074136
202 2009-01-04   38.0500  37705.00        PPC  990.932983
203 2009-01-11   45.4400  40670.00        PPC  895.026408
204 2009-01-18   39.9400  34947.00        PPC  874.987481
205 2009-01-25   35.0100  30595.00        PPC  873.893173
206 2009-02-01   38.4800  30555.00        PPC  794.048857
207 2009-02-08   43.7000  34171.00        PPC  781.945080
208 2009-02-15   38.5800  28972.00        PPC  750.959046
209 2009-02-22   37.3000  27115.00        PPC  726.943700
210 2009-03-01   41.8400  28075.00        PPC  671.008604
211 2009-03-08   55.3600  45067.00        PPC  814.071532
212 2008-03-30        NA        NA      Press          NA
213 2008-04-06  187.8800  24988.00      Press  132.999787
214 2008-04-13        NA        NA      Press          NA
215 2008-04-20 2271.6300  24988.00      Press   11.000031
216 2008-04-27   33.6300   6120.00      Press  181.980375
217 2008-05-04  146.6600  30065.00      Press  204.997954
218 2008-05-11  135.9200  43631.00      Press  321.005003
219 2008-05-18  188.7200  80019.00      Press  424.009114
220 2008-05-25  237.8000 236136.00      Press  993.002523
221 2008-06-01  204.0700  76934.00      Press  376.998089
222 2008-06-08  213.5700  66635.00      Press  312.005431
223 2008-06-15  190.7700  51509.00      Press  270.005766
224 2008-06-22  126.0200  23440.00      Press  186.002222
225 2008-06-29  228.7400  38428.00      Press  167.998601
226 2008-07-06  272.9300  51311.00      Press  188.000586
227 2008-07-13  302.3700  38704.00      Press  128.002117
228 2008-07-20  383.5400  31834.00      Press   83.000469
229 2008-07-27  186.7800   8592.00      Press   46.000642
230 2008-08-03        NA        NA      Press          NA
231 2008-08-10        NA        NA      Press          NA
232 2008-08-17  677.6500  27106.00      Press   40.000000
233 2008-08-24  546.1000  19660.00      Press   36.000732
234 2008-08-31  488.1800  33684.00      Press   68.999140
235 2008-09-07        NA        NA      Press          NA
236 2008-09-14 1935.7100  38714.00      Press   19.999897
237 2008-09-21  641.4200   7697.00      Press   11.999938
238 2008-09-28 1603.0000  27251.00      Press   17.000000
239 2008-10-05        NA        NA      Press          NA
240 2008-10-12 2584.0900  20673.00      Press    8.000108
241 2008-10-19 2156.0600  25873.00      Press   12.000130
242 2008-10-26  730.9200   6578.00      Press    8.999617
243 2008-11-02        NA        NA      Press          NA
244 2008-11-09  153.4800  29621.00      Press  192.995830
245 2008-11-16  183.3500  11551.00      Press   62.999727
246 2008-11-23  270.8000  20852.00      Press   77.001477
247 2008-11-30  239.8700  47734.00      Press  198.999458
248 2008-12-07  148.1600  37041.00      Press  250.006749
249 2008-12-14  125.0800  30894.00      Press  246.993924
250 2008-12-21        NA        NA      Press          NA
251 2008-12-28        NA        NA      Press          NA
252 2009-01-04   91.0100  37041.00      Press  406.999231
253 2009-01-11  168.9900  43430.00      Press  256.997455
254 2009-01-18  156.9500  33588.00      Press  214.004460
255 2009-01-25   30.1100   6444.00      Press  214.015277
256 2009-02-01        NA        NA      Press          NA
257 2009-02-08        NA        NA      Press          NA
258 2009-02-15        NA        NA      Press          NA
259 2009-02-22        NA        NA      Press          NA
260 2009-03-01        NA        NA      Press          NA
261 2008-03-30        NA        NA       DRTV          NA
262 2008-04-06        NA        NA       DRTV          NA
263 2008-04-13        NA        NA       DRTV          NA
264 2008-04-20        NA        NA       DRTV          NA
265 2008-04-27        NA        NA       DRTV          NA
266 2008-05-04        NA        NA       DRTV          NA
267 2008-05-11        NA        NA       DRTV          NA
268 2008-05-18        NA        NA       DRTV          NA
269 2008-05-25        NA        NA       DRTV          NA
270 2008-06-01  307.8801  23706.77       DRTV   77.000000
271 2008-06-08  224.0812  43247.67       DRTV  193.000000
272 2008-06-15  221.1121  48423.55       DRTV  219.000000
273 2008-06-22  222.3887  52261.34       DRTV  235.000000
274 2008-06-29  201.4904  42312.98       DRTV  210.000000
275 2008-07-06  197.2182  29779.95       DRTV  151.000000
276 2008-07-13  207.5627  36531.03       DRTV  176.000000
277 2008-07-20  178.0263  32578.82       DRTV  183.000000
278 2008-07-27  131.4428  21162.29       DRTV  161.000000
279 2008-08-03        NA        NA       DRTV          NA
280 2008-08-10        NA        NA       DRTV          NA
281 2008-08-17        NA        NA       DRTV          NA
282 2008-08-24        NA        NA       DRTV          NA
283 2008-08-31        NA        NA       DRTV          NA
284 2008-09-07        NA        NA       DRTV          NA
285 2008-09-14        NA        NA       DRTV          NA
286 2008-09-21        NA        NA       DRTV          NA
287 2008-09-28        NA        NA       DRTV          NA
288 2008-10-05        NA        NA       DRTV          NA
289 2008-10-12        NA        NA       DRTV          NA
290 2008-10-19        NA        NA       DRTV          NA
291 2008-10-26        NA        NA       DRTV          NA
292 2008-11-02        NA        NA       DRTV          NA
293 2008-11-09        NA        NA       DRTV          NA
294 2008-11-16        NA        NA       DRTV          NA
295 2008-11-23        NA        NA       DRTV          NA
296 2008-11-30        NA        NA       DRTV          NA
297 2008-12-07        NA        NA       DRTV          NA
298 2008-12-14        NA        NA       DRTV          NA
299 2008-12-21        NA        NA       DRTV          NA
300 2008-12-28        NA        NA       DRTV          NA
301 2009-01-04        NA        NA       DRTV          NA
302 2009-01-11        NA        NA       DRTV          NA
303 2009-01-18        NA        NA       DRTV          NA
304 2009-01-25        NA        NA       DRTV          NA
305 2009-02-01        NA        NA       DRTV          NA
306 2009-02-08        NA        NA       DRTV          NA
307 2009-02-15        NA        NA       DRTV          NA
308 2009-02-22        NA        NA       DRTV          NA
309 2009-03-01        NA        NA       DRTV          NA
310 2009-03-08        NA        NA       DRTV          NA
311 2009-03-15        NA        NA       DRTV          NA
312 2009-03-22        NA        NA       DRTV          NA

hadley wickham

unread,
Mar 24, 2009, 5:01:11 PM3/24/09
to simeon duckworth, ggplot2
Two options:

1. Make sure your aggregation function returns a data frame:

to.monthly <- function (df) {
df.z <- zoo(df[c("yy","zz")],df$Date)
df.zz<- aggregate(df.z,as.yearmon,sum,na.rm=T)
df.zz$CPA <- with(df.zz,ifelse(zz==0,0,yy/zz))
as.data.frame(df.zz)
}

2. Do the aggregation directly:

df.month <- ddply(weekly.df, .(Media, month = as.yearmon(Date)),
colwise(sum, .(yy, zz))
df.month$CPA <- with(df.month, yy / zz)
df.month[is.na(df.month)] <- 0

Regards,

Hadley
--
http://had.co.nz/
Reply all
Reply to author
Forward
0 new messages