ggplot with straight lines with different slopes and intercepts - r

Hi I want to include a series of straight lines with intercepts and slopes using ggplot. This is my df, which is my firts plot:
df
x y
1 5.767382e-04 -0.0179316822
2 5.382526e-04 -0.0064160490
3 3.283479e-04 0.0118829440
4 5.515362e-04 -0.0082115912
5 6.214837e-04 -0.0171607214
6 9.149512e-04 0.0162441461
7 1.096518e-03 0.0096310500
8 1.231807e-03 0.0039493755
9 1.291489e-03 -0.0373589167
10 1.290377e-03 -0.0047063656
11 1.346938e-03 0.0236590046
12 1.561922e-03 0.0063475614
13 1.421441e-03 -0.0070059435
14 1.546511e-03 -0.0169201470
15 -1.674733e-04 0.0015561601
16 1.954316e-03 0.0249988252
17 1.450279e-03 -0.0229110197
18 3.647360e-03 0.0196769910
19 -9.657013e-04 -0.0232707201
20 9.267196e-03 -0.0147603533
21 -2.330904e-03 0.0146431308
22 1.690023e-03 0.0144978266
23 -1.899995e-03 -0.0079710747
24 -3.216321e-03 0.0127135418
25 7.389830e-04 0.0251444785
26 5.605426e-04 -0.0076547396
27 7.745684e-04 0.0026117673
28 -4.348211e-04 0.0042256186
29 8.814315e-04 0.0169941536
30 -2.446243e-03 -0.0273569406
31 0.000000e+00 0.0074404985
32 1.252793e-02 -0.0239050021
33 1.859272e-03 0.0036494591
34 -3.281863e-05 0.0298095265
35 -5.894445e-03 -0.0118560733
36 -1.450642e-04 -0.0056893640
37 8.910996e-04 0.0263777220
38 -6.954649e-03 -0.0104581718
39 5.741444e-04 0.0193070220
40 -1.564921e-04 0.0458305555
41 -2.394386e-03 -0.0127560808
42 -4.719316e-03 0.0107511726
43 -4.819214e-03 0.0289323344
44 -8.151265e-04 -0.0160308497
45 -5.028173e-04 0.0180222018
46 -1.118739e-03 0.0240456897
47 -4.347277e-03 -0.0523667661
48 -6.275963e-04 0.0221451602
49 5.550606e-04 0.0129487351
50 4.902597e-04 0.0143767869
51 4.406320e-04 -0.0063753077
52 -3.324976e-03 0.0071569408
53 -7.674161e-04 0.0100497676
54 1.096383e-03 -0.0132797308
55 1.722997e-03 -0.0175982918
56 1.796998e-03 -0.0131551067
57 1.674058e-03 0.0105408021
58 2.762772e-04 0.0195710544
59 4.198189e-03 -0.0203275722
60 6.759233e-03 0.0293425895
61 1.623984e-03 -0.0007776215
62 2.687013e-04 -0.0067553441
63 1.764196e-03 -0.0231203356
64 2.067229e-03 -0.0040448936
65 7.313363e-04 -0.0409024742
66 1.827486e-03 -0.0245661039
67 -4.117068e-03 0.0098401969
68 1.761924e-03 -0.0164702633
69 1.468973e-02 0.0051605124
70 6.409529e-03 0.0054452684
71 2.160516e-03 -0.0340426755
72 5.874911e-03 -0.0207890110
73 -4.931250e-05 0.0426363179
74 -3.308259e-04 -0.0035186580
75 1.001841e-02 -0.0110782883
76 6.817267e-03 -0.0247737276
77 1.898683e-03 -0.0071688431
78 1.879390e-03 0.0262043639
79 -3.978326e-03 0.0161207659
80 2.038382e-03 -0.0491302989
81 2.952938e-03 -0.0211728961
82 -5.091002e-05 -0.0133934387
83 2.233294e-03 -0.0400734285
84 7.845835e-03 -0.0166611856
85 2.290868e-03 -0.0193531306
86 2.576246e-03 0.0097141933
87 8.855930e-03 0.0357254985
88 8.331340e-04 -0.0333207907
89 2.746399e-03 0.0109406292
90 5.894602e-03 -0.0027239561
91 2.760225e-03 0.0107796305
92 5.507580e-03 -0.0058818392
93 7.966446e-03 -0.0124912857
94 2.269484e-03 -0.0423110888
95 1.147931e-02 -0.0043869382
96 1.343029e-03 0.0177541412
97 3.332866e-03 -0.0207729389
98 7.214568e-03 0.0189228345
99 5.126573e-03 -0.0106763932
100 1.545817e-03 0.0468064583
101 6.923315e-04 0.0166643412
102 -5.572046e-03 0.0179240781
103 8.799100e-04 -0.0213355187
104 8.703026e-03 0.0165175080
105 5.011657e-03 -0.0006547804
106 -3.404681e-03 0.0227698214
107 7.859670e-03 0.0079283421
108 1.201645e-04 -0.0346891934
109 5.052620e-03 0.0147842433
110 6.510928e-03 0.0095857959
111 -6.805175e-03 -0.0468008862
112 -4.363319e-04 -0.0290832355
113 -8.097971e-03 0.0116832017
114 1.943374e-02 -0.0094214974
115 1.306860e-04 -0.0186188622
116 -2.380523e-03 0.0181519870
117 -4.110306e-04 -0.0208185537
118 2.305092e-03 -0.0308729855
119 8.793202e-03 0.0050425386
120 -2.153084e-04 -0.0001370091
121 -2.057392e-03 -0.0011947316
122 -2.924294e-03 -0.0008234326
123 5.883910e-05 -0.0194284696
124 -9.804020e-04 0.0109390697
125 1.857671e-03 -0.0141840698
126 3.811320e-04 -0.0127408263
127 4.298727e-03 0.0074098408
128 1.451233e-03 -0.0100142738
129 6.517445e-04 -0.0194408567
130 5.964974e-03 0.0102715503
131 -2.243700e-03 0.0323884648
132 -2.588121e-04 0.0013129366
133 1.569282e-03 -0.0006164677
134 1.709266e-03 0.0008549983
135 1.370448e-03 0.0035117342
136 6.829333e-03 0.0021959986
137 2.250341e-03 -0.0070402575
138 1.590869e-03 0.0046659934
139 3.164434e-03 0.0825476969
140 2.186351e-03 0.0230247530
141 -1.470575e-02 0.0981854863
142 -4.647970e-03 0.0516244670
143 -1.381099e-04 0.0650898115
144 -2.663386e-03 -0.0027354677
145 3.891808e-04 0.0113962573
146 1.282737e-04 -0.0055887228
147 1.006854e-02 -0.0332557676
148 3.609098e-03 -0.0845451850
149 9.674767e-05 -0.0210540954
150 -4.803852e-03 -0.0066589253
151 5.373557e-03 0.0170621188
152 -7.173718e-04 0.0188572127
153 3.751451e-03 -0.0301665839
154 1.813012e-04 0.0044078294
155 -4.622137e-03 0.0161162531
156 8.778474e-03 0.0077374492
157 2.243122e-04 0.0512243884
158 2.435534e-04 0.0036167504
159 4.313842e-03 -0.0090206012
160 2.140608e-03 0.0520850888
161 4.855610e-03 -0.0292653473
162 -6.378769e-03 -0.0457181372
163 1.409388e-04 0.0188185729
164 -6.677023e-03 -0.0249923532
165 -2.350696e-03 -0.0144915120
166 2.238066e-04 0.0262718350
167 2.985634e-03 -0.0053246849
168 1.733721e-02 -0.0227874598
169 1.110229e-02 -0.0153335154
170 1.218916e-02 0.0145804128
171 -1.636347e-03 -0.0397472603
172 4.168994e-04 0.0132717818
173 4.443091e-04 0.0249194075
174 2.261120e-03 -0.0222072628
175 3.282455e-04 0.0081088595
176 7.487101e-04 0.0442237830
177 -6.156654e-03 -0.0355019963
178 1.291197e-04 0.0259225094
179 7.723982e-03 0.0119600885
180 2.952351e-04 -0.0152999892
181 2.366808e-04 -0.0122095048
182 2.076462e-04 -0.0001917117
183 1.917117e-04 -0.0162039464
184 2.110784e-04 0.0535713964
185 1.539030e-04 -0.0188620346
186 7.841969e-05 0.0010503132
187 3.133568e-05 -0.0136614578
188 1.270608e-04 -0.0165115654
189 9.688358e-05 0.0023226192
190 9.665883e-05 0.0052383062
191 3.205231e-05 -0.0141548721
192 8.127108e-05 0.0249463151
193 1.585314e-04 0.0434959064
194 1.821383e-04 -0.0003188219
195 1.518326e-04 -0.0246884369
196 2.023110e-04 0.0262360490
197 2.122305e-04 -0.0003942620
198 2.123142e-04 0.0004245834
199 2.273813e-04 -0.0201528956
200 2.320096e-04 -0.0458005930
201 2.590716e-04 0.0079359095
202 2.248996e-04 -0.0193856392
203 2.293015e-04 -0.0110522023
204 1.987314e-04 0.0077707135
205 1.971933e-04 -0.0195328110
206 2.345923e-04 0.0052150485
207 2.167046e-04 -0.0127680943
208 2.363707e-04 0.0066644474
209 2.180312e-04 -0.0191872308
210 2.393490e-04 0.0126747424
211 2.532137e-04 0.0164598299
212 2.324770e-04 0.0241359061
213 2.593487e-04 0.0132217626
214 2.399482e-04 0.0213523358
215 1.722505e-04 0.0043441641
216 2.494504e-04 0.0374752187
217 1.802127e-04 -0.0285494293
218 2.317837e-04 0.0402110643
219 2.078076e-04 -0.0346015566
220 2.151232e-04 0.0070594834
221 2.136100e-04 -0.0301762391
222 2.044298e-04 0.0077859198
223 2.184462e-04 0.0145314016
224 1.537870e-04 0.0152173697
225 1.514647e-04 -0.0193785840
226 1.698697e-04 0.0256451675
227 -1.806522e-04 0.0210763020
228 -8.843818e-05 0.0383149856
229 -2.837040e-05 0.0177152551
230 -1.254312e-04 -0.0308801034
231 -8.624159e-05 -0.0190227662
232 -8.789792e-05 0.0064394355
ggplot(df, aes(x=df[,1], y=df[,2]))+ geom_point(shape=1) + xlim(-0.015,0.021) + ylim(-0.1,0.1)
Now I want to include straight with the intercepts and the slopes below. That is, there are 19 intercepts and 19 slopes, then I will have 19 lines:
intercept_slope
intercept slope
tau= 0.05 -0.0382845875 0.89386498
tau= 0.10 -0.0289286773 0.35422172
tau= 0.15 -0.0212578889 -0.08822473
tau= 0.20 -0.0193409306 -0.19879373
tau= 0.25 -0.0163342536 -0.37221704
tau= 0.30 -0.0130620457 -0.20459475
tau= 0.35 -0.0088296144 -0.45186425
tau= 0.40 -0.0058704748 -0.51982456
tau= 0.45 -0.0002945895 -0.46964248
tau= 0.50 0.0018460162 -0.57979144
tau= 0.55 0.0050322871 -0.74374709
tau= 0.60 0.0074404985 -0.70151564
tau= 0.65 0.0106591381 -0.63874335
tau= 0.70 0.0133004843 -0.63371325
tau= 0.75 0.0160149735 -0.89770765
tau= 0.80 0.0194564185 -0.97319036
tau= 0.85 0.0244394584 -1.30573010
tau= 0.90 0.0275024680 -1.26220009
tau= 0.95 0.0438769348 -2.40348955
I'm trying so but is wrong:
ggplot(df, aes(x=df[,1], y=df[,2]))+ geom_point(shape=1) + xlim(-0.015,0.021) + ylim(-0.1,0.1)+
for (i in 1:19) {
geom_abline(intercept = intercept_slope[i,1], slope = intercept_slope[i,2])
}
What am doing wrong? Any help?

Related

How to add empty rows where missing data has been removed

I have used lmlist to apply a common to grouped data (by Participant) and create a dataframe with each participant's coefficient (A), like so:
myCoefficients <- as.data.frame(coef(lmList(Outcome ~ A | Participant, mydata)))
This produces a dataframe that looks like this:
(Intercept) A
1 11.784913184 0.008224641
2 13.416552668 0.126538988
3 1.255375012 -0.261815119
4 -0.453204283 -0.050068500
5 1.855255007 0.019615941
6 18.233333333 0.266666667
7 10.622690151 0.039481512
8 4.583096557 0.056392969
9 -10.316190476 -0.327619048
10 -0.750918742 -0.011841568
11 0.000000000 0.000000000
12 1.658747938 -0.003200315
13 6.639959940 0.100150225
14 8.543573432 0.111088310
15 -0.409441233 0.075626204
16 0.000000000 0.000000000
17 16.391626950 0.338496534
18 -2.780630438 -0.119811724
19 3.581120944 0.057030482
20 -7.435064935 -0.086580087
21 0.000000000 0.000000000
22 -1.399803872 -0.041049967
23 0.000000000 0.000000000
24 5.748297340 0.073490890
25 2.502387775 0.022385387
26 5.477356181 -0.071399429
27 0.000000000 0.000000000
28 0.000000000 0.000000000
29 0.202822570 0.004236503
30 0.000000000 0.000000000
31 3.191532668 0.018318746
32 5.808669308 -0.089812308
33 15.556690047 0.288999378
34 6.044498212 0.033978540
35 5.384817738 0.088688463
36 2.429605338 -0.119020694
37 9.498121941 -0.068103350
38 1.449211455 -0.038649097
39 18.311828852 0.228294348
40 36.288255223 0.685770751
41 20.607074068 0.097268429
42 12.587294301 0.126299603
43 6.688188926 0.088422840
44 6.820835614 0.051997811
45 -2.063996902 -0.533215333
46 12.255847953 0.066520468
47 -4.818481848 -0.099009901
48 11.449166132 0.105355997
49 13.623012447 0.204422043
50 11.676916534 0.056099996
51 2.514750467 -0.019606775
52 0.125293117 -0.050347142
53 0.000000000 0.000000000
54 5.304979604 -0.175722320
55 10.318437929 0.096100382
56 0.000000000 0.000000000
57 10.607768097 -0.002279945
58 10.333968509 0.137398456
59 -4.513711889 -0.563639297
60 6.721815687 0.006860279
61 -0.718921180 -0.058085796
62 12.354598540 0.192189781
63 20.850979616 0.274787255
65 7.154075137 0.059849368
66 5.020082784 -0.008201748
67 0.229156161 -0.459531014
68 6.602570969 0.038832351
69 18.606677985 0.180531975
70 1.261939931 -0.064992614
71 0.000000000 0.000000000
72 8.565326633 0.105527638
73 6.025134650 0.065914337
74 0.411054480 -0.008632617
75 6.001972711 0.005753740
76 14.423697726 0.102622891
77 -1.782058047 -0.024274406
78 13.461871683 0.196417421
79 -2.490421456 -0.137691571
80 0.986939239 0.006814310
81 0.000000000 0.000000000
82 26.074865546 0.338946141
83 4.721769334 -0.023747076
84 3.491952414 0.055983205
85 8.621555769 0.111749489
86 13.298121427 0.175333515
87 5.075415244 -0.030621479
88 5.427200030 -0.056299149
89 5.784197111 0.052613361
90 2.967869893 -0.024593415
91 11.439869695 0.154194191
92 1.439169713 -0.137264690
93 0.000000000 0.000000000
94 5.696352440 0.077569872
95 2.544640478 -0.024518949
96 3.933483703 0.037261186
97 4.896524416 0.065283756
98 2.135022525 -0.374031801
99 7.190891371 0.083368454
100 23.054124552 0.531790023
101 6.161769255 -0.221620786
102 -14.547148289 -0.265139993
103 12.140619804 0.201380861
104 4.432939593 0.015699761
105 -0.837367221 -0.034716496
106 -0.122268163 -0.033668045
107 -5.696417101 -0.176646920
108 13.010822852 0.085412776
109 19.237564131 0.530002231
110 1.938517087 -0.178676770
111 6.888465629 -0.009047188
112 7.164846545 0.155902843
113 -0.403225806 -0.016129032
114 1.008421194 -0.058635633
115 3.170498084 0.028136973
116 7.475271328 0.008586344
117 9.387123820 0.130438620
118 -4.720329503 -0.102333977
119 -0.690008119 -0.122506379
120 9.928991185 -0.073457395
121 -8.166768363 -0.243770694
122 -0.836936555 -0.071781828
123 -13.756592007 -0.129834974
124 22.520513735 0.360226288
125 -6.268156425 -0.167597765
126 -7.517700552 -0.130074700
127 -14.041414611 -0.273382969
128 0.000000000 0.000000000
129 8.046064474 -0.259777450
130 3.669741697 -0.357933579
131 2.593244581 -0.190890087
132 -8.000000000 NA
133 8.328107184 -0.071265678
134 1.637694105 -0.192730521
135 3.693134192 -0.136592243
136 2.161687299 -0.180897599
137 0.000000000 0.000000000
138 0.721612005 -0.039410582
139 7.749737119 0.030494217
140 -5.808393153 -0.096355605
141 12.282297336 0.080170438
142 -5.316274128 -0.176288295
143 -4.441255140 -0.138249032
144 -1.117341518 -0.083225121
145 -0.752677582 -0.141942632
146 3.407083929 -0.101590819
147 6.265884172 0.002073376
148 -2.148945392 -0.152051430
149 28.415807560 0.554123711
150 8.716573171 0.118457600
151 12.496143959 0.088946015
152 19.149987332 0.217095262
153 0.009304822 -0.011094211
154 7.863488624 0.082204767
155 4.728299224 -0.416372618
156 4.624162598 0.035483138
157 8.492298276 0.117821075
158 -14.046175789 -0.229987520
159 1.183873993 0.017370732
160 0.000000000 0.000000000
161 7.045593921 0.066213394
162 5.513560300 0.031015580
163 4.244824402 -0.064298666
164 1.054528148 0.080908349
165 -11.086497890 -0.221518987
166 13.992083786 0.151239106
167 0.111296142 -0.093810713
168 -1.042661399 -0.045771688
169 16.828913325 0.164294955
170 1.743498818 -0.011820331
171 3.609741954 0.005798782
172 -2.110598319 0.011537828
173 -8.958189217 -0.714852492
174 -7.150518727 -0.141802105
175 10.645382098 0.141322541
176 -14.073564593 -0.445574163
177 16.096282103 0.289202798
178 8.649276860 0.162060950
179 0.000000000 0.000000000
180 -7.084494960 -0.158696118
181 -1.312672577 -0.103255341
182 -3.366945247 -0.223307037
183 14.938014695 0.208146358
184 -0.678981349 -0.185437590
185 10.027063876 0.030983707
186 12.853728050 0.126413533
187 -0.418968692 -0.010474217
188 7.242331839 0.175605381
189 -4.014336918 -0.143369176
190 10.229368645 0.118231260
191 -7.187500000 -0.539772727
192 25.785714286 0.464285714
193 15.189222323 0.136346677
194 12.845133231 0.164776481
195 -22.088160874 -0.266480224
196 0.000000000 0.000000000
197 0.000000000 0.000000000
198 1.362202049 -0.052177290
199 -11.184823000 -0.463622140
200 -0.859435330 -0.035683015
201 2.601550453 0.026826641
202 0.342018669 -0.125041972
204 5.335232543 0.084139393
205 20.000000000 NA
206 -0.375872383 -0.002492522
207 28.872131148 0.863934426
208 3.563577586 0.101939655
209 20.433887877 0.131835967
210 -5.815011124 -0.218884963
211 -11.266979501 -0.353173623
212 -11.173933501 -0.250941029
213 -0.056431693 -0.006460194
214 18.165829146 0.251256281
215 6.369295470 0.060584610
216 -3.322072072 -0.073198198
217 -2.677165354 0.078740157
218 0.898822629 -0.078491433
219 4.393285217 -0.030609615
220 6.165938069 0.054128719
221 15.223880597 0.179104478
222 22.583177570 0.337180008
223 15.386230059 0.304366079
224 -11.271916238 -0.118435454
225 9.214443626 0.087140752
226 13.651131824 -0.015978695
227 10.126607319 0.117705242
228 -4.100042391 -0.123781263
229 9.859298532 0.218189233
230 2.279411765 -0.489430147
231 7.042338217 0.066514042
232 -1.149144869 -0.020812374
233 3.501552079 -0.045179334
234 10.348929473 0.157197118
235 -4.309844723 -0.070597199
236 -0.406851091 0.064296520
237 0.000000000 0.000000000
238 -12.870644391 -0.185441527
239 6.572819573 0.091931533
240 146.000000000 7.800000000
241 59.560466246 1.131617290
242 -2.477952661 -0.017301454
243 7.670426344 0.195818054
244 4.464389692 0.030444692
245 0.000000000 0.000000000
246 -5.090371915 -0.281543274
247 3.318152764 0.031160115
248 2.089063523 -0.111329404
249 4.831575199 -0.003182445
250 3.528881195 0.044187318
251 -7.641223478 -0.186414990
252 8.691099476 0.314136126
253 7.673273132 0.161838865
254 11.152559139 0.074838199
255 -11.197466897 -0.077720207
256 1.761562470 0.020807718
257 12.047270872 0.192994389
258 3.223861983 0.014549990
259 -4.670398010 -0.061300640
260 1.468158966 0.014962892
261 3.374092944 0.074725953
262 0.000000000 0.000000000
263 11.308158651 0.131560838
264 30.000000000 NA
265 9.231769665 0.085299130
266 4.178069353 0.007966261
267 13.004952586 0.161534918
268 5.447510330 0.099796905
269 0.000000000 0.000000000
270 9.579288026 0.115580213
271 16.908915559 0.311615945
272 9.610721530 -0.059055346
273 9.265092417 0.141893316
274 -7.569751715 -0.282050043
275 5.496770989 -0.041728763
276 7.409235108 0.163905534
277 -1.593325458 -0.018606025
278 5.703958450 0.002105559
279 17.006838906 0.118920973
280 -3.320901995 0.143972246
281 30.029290374 0.623751831
282 -2.678965618 -0.022542195
283 24.687500000 0.312500000
284 -15.059602649 -0.238410596
286 0.000000000 NA
287 -2.157762938 -0.045909850
288 -7.806357888 -0.209365920
289 11.782270607 0.287713841
290 4.821551496 0.065395817
291 17.686923508 0.085167915
292 6.617959072 0.108308808
293 14.685806258 -0.169112143
294 11.424153771 0.142710499
295 2.513812155 0.041436464
296 19.254025911 0.275941397
297 11.448286941 0.142535853
298 -5.008457711 -0.162106136
299 -1.435754190 -0.140229741
300 23.478882079 0.311710847
The indexing column on the far left contains the Participant ID numbers. While there are only 297 rows in myCoefficients, the Participant IDs range from 1-300. This is because, for example, participant 285 and two others were removed from the analysis at an earlier stage.
How can I add empty rows were the missing participants are?
For example, where participant 285 is, I would like to see this:
...
283 24.687500000 0.312500000
284 -15.059602649 -0.238410596
285 NA NA
286 0.000000000 NA
287 -2.157762938 -0.045909850
...
Is there some way to use the indexing column, even though R doesn't recognise it as a column? i.e.:
ncol(myCoefficients)
[1] 2
As mentioned by #Simon Woodward, those are rownames, using tidyverse we can use complete to complete the missing rows with NA's.
library(tidyverse)
df %>%
rownames_to_column('row') %>%
mutate(row = as.integer(row)) %>%
complete(row = seq(min(row) : max(row))) %>%
select(-row)
# A tibble: 14 x 2
# A B
# <dbl> <dbl>
# 1 11.8 0.00822
# 2 13.4 0.127
# 3 1.26 -0.262
# 4 NA NA
# 5 -0.453 -0.0501
# 6 1.86 0.0196
# 7 18.2 0.267
# 8 10.6 0.0395
# 9 NA NA
#10 NA NA
#11 4.58 0.0564
#12 -10.3 -0.328
#13 -0.751 -0.0118
#14 0 0
data
Tested on this small subset of data
df <- structure(list(A = c(11.784913184, 13.416552668, 1.255375012,
-0.453204283, 1.855255007, 18.233333333, 10.622690151, 4.583096557,
-10.316190476, -0.750918742, 0), B = c(0.008224641, 0.126538988,
-0.261815119, -0.0500685, 0.019615941, 0.266666667, 0.039481512,
0.056392969, -0.327619048, -0.011841568, 0)), class = "data.frame", row.names = c("1",
"2", "3", "5", "6", "7", "8", "11", "12", "13", "14"))
Using dplyr and tibble:
library(dplyr)
library(tibble)
# list of participants
mydata <- tibble(n = 1:300)
# convert rownames to numeric column
myCoefficients <- myCoefficients %>%
rownames_to_column() %>%
mutate(rowname = as.numeric(rowname))
# join myCoefficients to participants
result <- left_join(mydata, myCoefficients, by = c("n" = "rowname"))
Here is a way:
library(dplyr)
seq_merge <- data.frame(a = seq(1:length(rownames(myCoefficients))))
myCoefficients$a <- rownames(myCoefficients)
final <- merge(seq_merge,myCoefficients,all.x = T) %>%
select(-a)
> final
(Intercept) A
1 11.784913184 0.008224641
2 13.416552668 0.126538988
3 1.255375012 -0.261815119
4 -0.453204283 -0.050068500
5 1.855255007 0.019615941
6 18.233333333 0.266666667
7 10.622690151 0.039481512
8 4.583096557 0.056392969
9 -10.316190476 -0.327619048
10 -0.750918742 -0.011841568
11 0.000000000 0.000000000
12 1.658747938 -0.003200315
13 6.639959940 0.100150225
14 8.543573432 0.111088310
15 -0.409441233 0.075626204
16 0.000000000 0.000000000
17 16.391626950 0.338496534
18 -2.780630438 -0.119811724
19 3.581120944 0.057030482
20 -7.435064935 -0.086580087
21 0.000000000 0.000000000
22 -1.399803872 -0.041049967
23 0.000000000 0.000000000
24 5.748297340 0.073490890
25 2.502387775 0.022385387
26 5.477356181 -0.071399429
27 0.000000000 0.000000000
28 0.000000000 0.000000000
29 0.202822570 0.004236503
30 0.000000000 0.000000000
31 3.191532668 0.018318746
32 5.808669308 -0.089812308
33 15.556690047 0.288999378
34 6.044498212 0.033978540
35 5.384817738 0.088688463
36 2.429605338 -0.119020694
37 9.498121941 -0.068103350
38 1.449211455 -0.038649097
39 18.311828852 0.228294348
40 36.288255223 0.685770751
41 20.607074068 0.097268429
42 12.587294301 0.126299603
43 6.688188926 0.088422840
44 6.820835614 0.051997811
45 -2.063996902 -0.533215333
46 12.255847953 0.066520468
47 -4.818481848 -0.099009901
48 11.449166132 0.105355997
49 13.623012447 0.204422043
50 11.676916534 0.056099996
51 2.514750467 -0.019606775
52 0.125293117 -0.050347142
53 0.000000000 0.000000000
54 5.304979604 -0.175722320
55 10.318437929 0.096100382
56 0.000000000 0.000000000
57 10.607768097 -0.002279945
58 10.333968509 0.137398456
59 -4.513711889 -0.563639297
60 6.721815687 0.006860279
61 -0.718921180 -0.058085796
62 12.354598540 0.192189781
63 20.850979616 0.274787255
64 NA NA
65 7.154075137 0.059849368
66 5.020082784 -0.008201748
67 0.229156161 -0.459531014
68 6.602570969 0.038832351
69 18.606677985 0.180531975
70 1.261939931 -0.064992614
71 0.000000000 0.000000000
72 8.565326633 0.105527638
73 6.025134650 0.065914337
74 0.411054480 -0.008632617
75 6.001972711 0.005753740
76 14.423697726 0.102622891
77 -1.782058047 -0.024274406
78 13.461871683 0.196417421
79 -2.490421456 -0.137691571
80 0.986939239 0.006814310
81 0.000000000 0.000000000
82 26.074865546 0.338946141
83 4.721769334 -0.023747076
84 3.491952414 0.055983205
85 8.621555769 0.111749489
86 13.298121427 0.175333515
87 5.075415244 -0.030621479
88 5.427200030 -0.056299149
89 5.784197111 0.052613361
90 2.967869893 -0.024593415
91 11.439869695 0.154194191
92 1.439169713 -0.137264690
93 0.000000000 0.000000000
94 5.696352440 0.077569872
95 2.544640478 -0.024518949
96 3.933483703 0.037261186
97 4.896524416 0.065283756
98 2.135022525 -0.374031801
99 7.190891371 0.083368454
100 23.054124552 0.531790023
101 6.161769255 -0.221620786
102 -14.547148289 -0.265139993
103 12.140619804 0.201380861
104 4.432939593 0.015699761
105 -0.837367221 -0.034716496
106 -0.122268163 -0.033668045
107 -5.696417101 -0.176646920
108 13.010822852 0.085412776
109 19.237564131 0.530002231
110 1.938517087 -0.178676770
111 6.888465629 -0.009047188
112 7.164846545 0.155902843
113 -0.403225806 -0.016129032
114 1.008421194 -0.058635633
115 3.170498084 0.028136973
116 7.475271328 0.008586344
117 9.387123820 0.130438620
118 -4.720329503 -0.102333977
119 -0.690008119 -0.122506379
120 9.928991185 -0.073457395
121 -8.166768363 -0.243770694
122 -0.836936555 -0.071781828
123 -13.756592007 -0.129834974
124 22.520513735 0.360226288
125 -6.268156425 -0.167597765
126 -7.517700552 -0.130074700
127 -14.041414611 -0.273382969
128 0.000000000 0.000000000
129 8.046064474 -0.259777450
130 3.669741697 -0.357933579
131 2.593244581 -0.190890087
132 -8.000000000 NA
133 8.328107184 -0.071265678
134 1.637694105 -0.192730521
135 3.693134192 -0.136592243
136 2.161687299 -0.180897599
137 0.000000000 0.000000000
138 0.721612005 -0.039410582
139 7.749737119 0.030494217
140 -5.808393153 -0.096355605
141 12.282297336 0.080170438
142 -5.316274128 -0.176288295
143 -4.441255140 -0.138249032
144 -1.117341518 -0.083225121
145 -0.752677582 -0.141942632
146 3.407083929 -0.101590819
147 6.265884172 0.002073376
148 -2.148945392 -0.152051430
149 28.415807560 0.554123711
150 8.716573171 0.118457600
151 12.496143959 0.088946015
152 19.149987332 0.217095262
153 0.009304822 -0.011094211
154 7.863488624 0.082204767
155 4.728299224 -0.416372618
156 4.624162598 0.035483138
157 8.492298276 0.117821075
158 -14.046175789 -0.229987520
159 1.183873993 0.017370732
160 0.000000000 0.000000000
161 7.045593921 0.066213394
162 5.513560300 0.031015580
163 4.244824402 -0.064298666
164 1.054528148 0.080908349
165 -11.086497890 -0.221518987
166 13.992083786 0.151239106
167 0.111296142 -0.093810713
168 -1.042661399 -0.045771688
169 16.828913325 0.164294955
170 1.743498818 -0.011820331
171 3.609741954 0.005798782
172 -2.110598319 0.011537828
173 -8.958189217 -0.714852492
174 -7.150518727 -0.141802105
175 10.645382098 0.141322541
176 -14.073564593 -0.445574163
177 16.096282103 0.289202798
178 8.649276860 0.162060950
179 0.000000000 0.000000000
180 -7.084494960 -0.158696118
181 -1.312672577 -0.103255341
182 -3.366945247 -0.223307037
183 14.938014695 0.208146358
184 -0.678981349 -0.185437590
185 10.027063876 0.030983707
186 12.853728050 0.126413533
187 -0.418968692 -0.010474217
188 7.242331839 0.175605381
189 -4.014336918 -0.143369176
190 10.229368645 0.118231260
191 -7.187500000 -0.539772727
192 25.785714286 0.464285714
193 15.189222323 0.136346677
194 12.845133231 0.164776481
195 -22.088160874 -0.266480224
196 0.000000000 0.000000000
197 0.000000000 0.000000000
198 1.362202049 -0.052177290
199 -11.184823000 -0.463622140
200 -0.859435330 -0.035683015
201 2.601550453 0.026826641
202 0.342018669 -0.125041972
203 NA NA
204 5.335232543 0.084139393
205 20.000000000 NA
206 -0.375872383 -0.002492522
207 28.872131148 0.863934426
208 3.563577586 0.101939655
209 20.433887877 0.131835967
210 -5.815011124 -0.218884963
211 -11.266979501 -0.353173623
212 -11.173933501 -0.250941029
213 -0.056431693 -0.006460194
214 18.165829146 0.251256281
215 6.369295470 0.060584610
216 -3.322072072 -0.073198198
217 -2.677165354 0.078740157
218 0.898822629 -0.078491433
219 4.393285217 -0.030609615
220 6.165938069 0.054128719
221 15.223880597 0.179104478
222 22.583177570 0.337180008
223 15.386230059 0.304366079
224 -11.271916238 -0.118435454
225 9.214443626 0.087140752
226 13.651131824 -0.015978695
227 10.126607319 0.117705242
228 -4.100042391 -0.123781263
229 9.859298532 0.218189233
230 2.279411765 -0.489430147
231 7.042338217 0.066514042
232 -1.149144869 -0.020812374
233 3.501552079 -0.045179334
234 10.348929473 0.157197118
235 -4.309844723 -0.070597199
236 -0.406851091 0.064296520
237 0.000000000 0.000000000
238 -12.870644391 -0.185441527
239 6.572819573 0.091931533
240 146.000000000 7.800000000
241 59.560466246 1.131617290
242 -2.477952661 -0.017301454
243 7.670426344 0.195818054
244 4.464389692 0.030444692
245 0.000000000 0.000000000
246 -5.090371915 -0.281543274
247 3.318152764 0.031160115
248 2.089063523 -0.111329404
249 4.831575199 -0.003182445
250 3.528881195 0.044187318
251 -7.641223478 -0.186414990
252 8.691099476 0.314136126
253 7.673273132 0.161838865
254 11.152559139 0.074838199
255 -11.197466897 -0.077720207
256 1.761562470 0.020807718
257 12.047270872 0.192994389
258 3.223861983 0.014549990
259 -4.670398010 -0.061300640
260 1.468158966 0.014962892
261 3.374092944 0.074725953
262 0.000000000 0.000000000
263 11.308158651 0.131560838
264 30.000000000 NA
265 9.231769665 0.085299130
266 4.178069353 0.007966261
267 13.004952586 0.161534918
268 5.447510330 0.099796905
269 0.000000000 0.000000000
270 9.579288026 0.115580213
271 16.908915559 0.311615945
272 9.610721530 -0.059055346
273 9.265092417 0.141893316
274 -7.569751715 -0.282050043
275 5.496770989 -0.041728763
276 7.409235108 0.163905534
277 -1.593325458 -0.018606025
278 5.703958450 0.002105559
279 17.006838906 0.118920973
280 -3.320901995 0.143972246
281 30.029290374 0.623751831
282 -2.678965618 -0.022542195
283 24.687500000 0.312500000
284 -15.059602649 -0.238410596
285 NA NA
286 0.000000000 NA
287 -2.157762938 -0.045909850
288 -7.806357888 -0.209365920
289 11.782270607 0.287713841
290 4.821551496 0.065395817
291 17.686923508 0.085167915
292 6.617959072 0.108308808
293 14.685806258 -0.169112143
294 11.424153771 0.142710499
295 2.513812155 0.041436464
296 19.254025911 0.275941397
297 11.448286941 0.142535853
In base you can use match to fill the missing rows like:
#Using length of x
y[match(seq_len(nrow(x)), rownames(y)),]
# i a
#1 1 5
#2 2 4
#NA NA NA
#4 4 2
#5 5 1
#Using rownames of y
y[match(min(as.numeric(rownames(y))):max(as.numeric(rownames(y))), rownames(y)),]
# i a
#1 1 5
#2 2 4
#NA NA NA
#4 4 2
#5 5 1
Data:
x <- data.frame(i=1:5, a=5:1)
y <- x[-3,]
y
# i a
#1 1 5
#2 2 4
#4 4 2
#5 5 1

How to make intensity graph with outliers?

I have some data taken from a moving instrument through the water and flow data were logged every 0.5 seconds.I need to make a graph showing flow along the path of the instrument with different colors for selected range of flow (lets say 0.1 -1.5) and all outliers in same color.
How can I use R to plot the graph?
Here's some of my data:
idNr flow dep
21 0.135714532 3.16
22 0.131061729 3.07
23 0.13299406 3.11
24 0.145316675 3.1
25 6.31297442 3.07
26 0.331310509 3.14
27 0.445034592 3.17
28 0.637348777 3.45
29 0.87382414 4.04
30 1.302061623 5.31
31 1.80235436 6.78
32 1.63399146 8.24
33 1.675284308 9.78
34 1.686855996 11.27
35 1.775785232 12.72
36 1.455096956 14.22
37 1.530298919 15.69
38 1.431618958 17.15
39 1.446519477 18.7
40 1.532840436 20.15
41 1.595988278 21.55
42 1.478074882 23.07
43 1.545724299 24.5
44 1.475195233 26.05
45 1.542920138 27.56
46 1.437394899 29.02
47 1.644596033 30.59
48 2.303170426 31.94
49 1.77077097 33.5
50 1.530637295 34.97
51 1.439630621 36.41
52 1.469339767 37.9
53 1.330177211 39.36
54 1.478639264 40.85
55 1.465239548 42.3
56 1.536369473 43.85
57 1.484045946 45.39
58 1.561566967 46.92
59 1.487268243 48.5
60 1.509553327 50.01
61 1.436443415 51.42
62 1.520071435 52.98
63 1.455013499 54.55
64 1.472445826 56.01
65 1.499715554 57.48
66 1.514167783 58.99
67 1.493601132 60.62
68 1.531585488 62.05
69 1.625335896 63.61
70 1.478178989 65.08
71 1.531871974 66.57
72 1.472113782 68.16
73 1.4799859 69.58
74 1.458177137 71.21
75 1.591356624 72.79
76 1.542120401 74.28
77 1.694959183 75.77
78 1.720245831 77.25
79 1.519968728 78.75
80 1.390172108 80.34
81 1.520286586 81.81
82 1.592769579 83.38
83 1.632539512 84.88
84 1.481495103 86.41
85 1.529086844 87.98
86 1.536760058 89.52
87 1.52298084 91.03
88 1.731281442 92.53
89 1.639074839 94.02
90 1.562987505 95.53
91 1.543290194 97.04
92 1.578430537 98.61
93 1.702396728 100.12
94 1.657955781 101.6
95 1.557872012 103.16
96 1.613944568 104.68
97 1.631505361 106.16
98 1.435526209 107.66
99 1.711407354 109.26
100 1.57266259 110.72
101 1.514305998 112.27
102 1.56082106 113.78
103 1.828251113 115.27
104 1.748255115 116.76
105 1.854233769 118.3
106 1.803737202 119.75
107 1.67996921 121.25
108 1.751109178 122.77
109 1.76849805 124.32
110 1.758307258 125.82
111 1.740444751 127.32
112 1.644748694 128.81
113 1.620253049 130.29
114 1.75889143 131.77
115 1.760015837 133.24
116 1.683797088 134.78
117 1.713609054 136.31
118 1.26352548 137.78
119 1.81112139 139.37
120 1.888694446 140.83
121 1.774687553 142.36
122 1.739557437 143.78
123 1.64517875 145.33
124 1.699596858 146.8
125 1.628577412 148.35
126 1.769012673 149.81
127 1.594415839 151.41
128 1.493148224 152.89
129 1.581041449 154.42
130 1.538720671 155.93
131 1.589420092 157.41
132 1.64016166 158.93
133 1.575397227 160.43
134 1.63183131 162
135 1.75038462 163.44
136 1.434958447 165.01
137 1.74120127 166.5
138 1.748106592 167.98
139 1.813005453 169.46
140 1.541089106 170.98
141 1.556216895 172.56
142 1.660628956 174.08
143 1.693981673 175.61
144 1.67059241 177.09
145 1.66300418 178.66
146 1.652198157 180.17
147 1.709649777 181.65
148 1.745386082 183.15
149 1.385201724 184.62
150 1.468321001 186.15
151 1.627495534 187.58
152 1.678188454 189.03
153 1.810850273 190.55
154 1.585102162 192.01
155 1.652869637 193.48
156 1.593472296 195.03
157 1.846131262 196.53
158 1.442232687 198.03
159 1.279801142 199.57
160 1.803737202 201.06
161 1.794407014 202.61
162 1.456371696 204
163 1.815429315 205.38
164 1.518992563 206.89
165 1.647235482 208.41
166 1.47721908 209.86
167 1.698562049 211.35
168 0.835645183 211.94
169 0.971816361 211.79
170 0.215360462 212.01
171 0.576920795 212.05
172 0.504199289 212.02
173 0.352668234 212.02
174 0.503149022 211.89
175 0.180540198 212
176 0.242642996 211.91
177 0.132911363 211.98
178 0.131504237 212
179 0.131893716 211.96
180 0.132283195 211.9
181 0.132672674 212.02
182 0.919043761 212.02
183 1.705414848 211.94
184 2.491785935 211.9
185 0.187914127 211.97
186 0.137465923 211.98
187 0.490799032 211.85
188 0.695088396 211.6
189 0.283676082 211
190 0.965362936 210.07
191 0.769205485 209.16
192 1.41417407 208.1
193 1.437885765 207.08
194 1.359908615 206.02
195 1.311925665 204.77
196 1.239993728 203.57
197 1.352713698 202.38
198 1.454984487 201.12
199 1.07880741 199.89
200 1.171552813 198.65
201 1.32237999 197.41
202 1.354385018 196.15
203 1.090512744 194.97
204 1.390356612 193.71
205 1.213781005 192.33
206 1.431367612 191.08
207 1.384149391 189.81
208 1.282003839 188.57
209 1.332777243 187.45
210 1.235528323 186.19
211 1.110193788 184.9
212 1.247866678 183.7
213 1.193442015 182.56
214 1.313545026 181.36
215 1.284521151 180.08
216 1.258253835 178.91
217 1.263683914 177.68
218 1.331011134 176.49
219 1.234639947 175.26
220 1.338309201 174.07
221 1.224766564 172.91
222 1.280519417 171.66
223 1.150706617 170.44
224 1.260841259 169.25
225 1.09289038 168.05
226 1.262740102 166.84
227 1.170657422 165.68
228 1.176689196 164.49
229 1.262806038 163.31
230 1.40733017 162.17
231 1.24825008 160.93
232 1.282003839 159.8
233 1.237756113 158.62
234 1.339447054 157.48
235 1.383181118 156.3
236 1.213781005 155.12
237 1.310221259 154.01
238 1.344108343 152.81
239 1.367520862 151.66
240 1.197841162 150.49
241 1.219617617 149.28
242 1.299869434 148.11
243 1.370740857 146.95
244 1.175491535 145.83
245 1.272503374 144.68
246 1.251364988 143.52
247 1.321138129 142.3
248 1.127328278 141.16
249 1.319040773 139.94
250 1.118599826 138.74
251 1.312986113 137.64
252 1.392439005 136.41
253 1.197514718 135.29
254 1.218054343 134.14
255 1.230533733 132.94
256 1.209399639 131.82
257 1.362942371 130.63
258 1.365576729 129.47
259 1.235870422 128.24
260 1.250791647 127.08
261 1.407273491 125.89
262 1.279498015 124.77
263 1.419950309 123.67
264 1.343802584 122.51
265 1.359908615 121.28
266 1.221997022 120.16
267 1.406570776 118.98
268 1.32583614 117.77
269 1.456010534 116.59
270 1.446890774 115.47
271 1.486626504 114.25
272 1.271696732 113.07
273 1.123066893 111.94
Something like below?
# cut the flow data into range and add that into new "group" column
df$group <- cut(df$flow, c(0.1,1.5,max(df$flow)), labels=c("group1","group2"))
# plot the graph
library(ggplot2); library(dplyr)
ggplot(df, aes(x=idNr, y=flow)) +
geom_point(data = df %>% filter(group == "group1"), aes(colour=flow)) +
scale_colour_gradient(low="green", high="red", name="Not Outliers") +
geom_point(data = df %>% filter(group == "group2"), aes(size="Outliers"), colour="blue") +
guides(size = guide_legend("Outliers"))
Edit
per comment from OP.
If use dep as Y-axis, i.e. replace flow with dep in code (for ggplot) above,
graph will be

R calculate the average of one column corresponding to each bin of another column [duplicate]

This question already has an answer here:
R aggregate data in one column based on 2 other columns
(1 answer)
Closed 9 years ago.
I have these data that has two columns. As you can see in the graph, the data has too much noise. So, I want to discretize column "r" with size 5, and assign each row to its corresponding bin, then calculate the average of f for each bin.
> dr
r f
1 65.06919 21.796
2 62.36986 22.836
3 59.81639 22.980
4 57.42822 22.061
5 55.22681 21.012
6 53.23533 21.274
7 51.47815 21.594
8 49.98000 22.117
9 48.76474 20.366
10 47.85394 18.991
11 47.26521 20.920
12 47.01064 20.161
13 47.09565 22.328
14 47.51842 19.610
15 48.27007 18.615
16 49.33559 21.753
17 50.69517 22.754
18 52.32590 22.096
19 54.20332 22.020
20 56.30275 22.111
21 58.60034 21.395
22 61.07373 22.635
23 63.70243 22.128
24 66.46804 21.698
25 62.24147 21.879
26 59.41380 21.637
27 56.72742 21.991
28 54.20332 21.535
29 51.86521 21.093
30 49.73932 20.496
31 47.85394 21.737
32 46.23851 21.890
33 44.92215 21.236
34 43.93177 19.997
35 43.28972 19.661
36 43.01163 20.692
37 43.10452 19.663
38 43.56604 19.273
39 44.38468 20.743
40 45.54119 22.604
41 47.01064 22.167
42 48.76474 20.427
43 50.77401 21.543
44 53.00943 21.391
45 55.44367 21.313
46 58.05170 22.501
47 60.81118 22.414
48 63.70243 22.920
49 59.54830 21.571
50 56.58622 22.454
51 53.75872 22.643
52 51.08816 20.219
53 48.60041 20.300
54 46.32494 19.832
55 44.29447 20.284
56 42.54409 21.284
57 41.10961 21.350
58 40.02499 20.784
59 39.31921 20.383
60 39.01282 20.508
61 39.11521 19.413
62 39.62323 20.043
63 40.52160 18.583
64 41.78516 19.512
65 43.38202 20.849
66 45.27693 21.349
67 47.43416 20.734
68 49.81967 22.055
69 52.40229 22.108
70 55.15433 23.184
71 58.05170 23.147
72 61.07373 23.207
73 57.00877 21.467
74 53.90733 21.549
75 50.93133 23.035
76 48.10405 20.684
77 45.45327 20.189
78 43.01163 19.304
79 40.81666 19.739
80 38.91015 20.976
81 37.33631 21.305
82 36.13862 21.319
83 35.35534 20.133
84 35.01428 20.179
85 35.12834 20.634
86 35.69314 22.478
87 36.68787 21.608
88 38.07887 20.964
89 39.82462 18.409
90 41.88078 20.627
91 44.20407 20.980
92 46.75468 22.206
93 49.49747 21.828
94 52.40229 20.844
95 55.44367 21.619
96 58.60034 21.498
97 54.64430 19.433
98 51.40039 21.293
99 48.27007 20.687
100 45.27693 21.377
101 42.44997 21.282
102 39.82462 20.910
103 37.44329 18.810
104 35.35534 21.223
105 33.61547 20.197
106 32.28002 20.765
107 31.40064 19.781
108 31.01612 20.536
109 31.14482 21.245
110 31.78050 21.117
111 32.89377 20.303
112 34.43835 20.795
113 36.35932 20.754
114 38.60052 21.025
115 41.10961 20.924
116 43.84062 21.475
117 46.75468 21.435
118 49.81967 20.380
119 53.00943 21.590
120 56.30275 20.743
121 52.47857 20.600
122 49.09175 20.818
123 45.80393 21.514
124 42.63801 21.922
125 39.62323 21.469
126 36.79674 22.186
127 34.20526 19.625
128 31.90611 19.703
129 29.96665 18.793
130 28.46050 18.912
131 27.45906 19.239
132 27.01851 18.467
133 27.16616 18.974
134 27.89265 20.090
135 29.15476 19.155
136 30.88689 20.526
137 33.01515 20.273
138 35.46830 19.956
139 38.18377 21.547
140 41.10961 21.260
141 44.20407 20.802
142 47.43416 19.719
143 50.77401 21.645
144 54.20332 18.957
145 50.53712 21.410
146 47.01064 20.536
147 43.56604 20.963
148 40.22437 20.775
149 37.01351 22.257
150 33.97058 21.868
151 31.14482 18.907
152 28.60070 19.644
153 26.41969 17.694
154 24.69818 17.883
155 23.53720 17.975
156 23.02173 18.778
157 23.19483 18.896
158 24.04163 19.561
159 25.49510 20.137
160 27.45906 19.922
161 29.83287 19.574
162 32.52691 19.029
163 35.46830 20.356
164 38.60052 20.330
165 41.88078 20.005
166 45.27693 20.006
167 48.76474 21.056
168 52.32590 20.143
169 48.84670 22.094
170 45.18849 21.252
171 41.59327 22.023
172 38.07887 21.563
173 34.66987 21.408
174 31.40064 21.334
175 28.31960 19.855
176 25.49510 18.648
177 23.02173 17.397
178 21.02380 17.311
179 19.64688 16.714
180 19.02630 18.152
181 19.23538 18.187
182 20.24846 19.910
183 21.95450 20.451
184 24.20744 19.820
185 26.87006 19.862
186 29.83287 19.987
187 33.01515 19.363
188 36.35932 19.498
189 39.82462 19.121
190 43.38202 20.479
191 47.01064 20.311
192 50.69517 21.666
193 47.43416 21.995
194 43.65776 23.158
195 39.92493 24.632
196 36.24914 23.273
197 32.64966 22.535
198 29.15476 19.933
199 25.80698 18.277
200 22.67157 16.169
So, to walk trhough the procedure, looking at each row starting from row 1 would be assigned to bin [65-70], row 2 would be on in [60-65] ...
then for the final result, I want the middle point of each bin and the average of its f values. S, with that I can draw a line for f as a function of f(r)
As #Fernando already mentioned in his comment you could try cut (binning) and tapply:
tapply(df$f, cut(df$r, seq(15, 70, by=5)), mean)
# (15,20] (20,25] (25,30] (30,35] (35,40] (40,45] (45,50] (50,55] (55,60] (60,65] (65,70]
#17.68433 18.55918 19.28683 20.49000 20.87942 20.65430 20.96155 21.35146 21.92259 22.57414 21.74700
Alternatively, you can use the wonderful plyr package.
library(plyr)
ddply(df, .(cut(df$r, 5)), colwise(mean))
However, if you have to ask a question like the above, you are just fine with the tapply solution.

R aggregate data in one column based on 2 other columns

So, I have these data given below, and my goal is to aggregate column v3 in terms of columns v1 and v2 and add the v3 values for each bin of v1 and v2. For example, the first line correspond to interval v1=21, v2=16, so the value of v3 will be aggregated over its (v1,v2) interval. And repeat this for the rest of rows. I want to use the mean as the aggregation function!
> df
v1 v2 v3
1 21.359 16.234 24.283
2 47.340 9.184 21.328
3 35.363 -13.258 14.556
4 -29.888 14.154 17.718
5 -10.109 -16.994 20.200
6 -32.387 1.722 15.735
7 49.240 -5.266 17.601
8 -38.933 2.558 16.377
9 41.213 5.937 21.654
10 -33.287 -4.028 19.525
11 -10.223 11.961 16.756
12 -48.652 16.558 20.800
13 44.778 27.741 17.793
14 -38.546 29.708 13.948
15 -45.622 4.729 17.793
16 -36.290 12.383 18.014
17 -19.626 19.767 18.182
18 -32.248 29.480 15.108
19 -41.859 35.502 8.490
20 -36.058 21.191 16.714
21 -23.588 0.524 21.471
22 -24.423 39.963 18.257
23 -0.042 -45.899 17.654
24 -35.479 32.049 9.294
25 -24.632 20.603 17.757
26 -26.591 25.882 18.968
27 -34.364 43.959 13.905
28 -19.334 29.728 20.102
29 12.304 -39.997 17.002
30 0.958 37.162 20.779
31 -35.475 -40.611 14.719
32 -39.268 44.382 11.247
33 -10.154 39.053 19.458
34 -12.612 32.056 17.759
35 2.730 -1.473 20.228
36 -45.326 -52.299 9.305
37 -1.996 -15.551 13.295
38 -26.655 -37.319 19.148
39 -18.509 -30.047 18.889
40 -22.705 -25.577 19.007
41 -15.705 -15.397 19.112
42 -2.637 9.790 10.548
43 -14.107 -3.145 19.654
44 -29.272 -19.906 18.503
45 -9.569 -4.632 11.334
46 2.114 18.048 14.744
47 -4.241 16.073 15.420
48 31.869 -3.394 21.559
49 20.425 35.205 22.250
50 -18.605 -8.866 20.082
51 -26.677 -7.690 21.850
52 -5.240 4.805 11.399
53 -6.766 2.538 6.292
54 4.567 22.554 19.682
55 -20.701 6.430 20.996
56 -23.972 16.141 17.976
57 -6.651 24.048 18.082
58 -32.243 -6.100 19.517
59 2.236 29.736 19.667
60 18.830 15.586 15.969
61 -9.598 28.414 17.806
62 -30.825 12.194 22.346
63 -17.415 15.795 18.135
64 -14.823 5.931 17.915
65 -14.234 12.882 13.001
66 9.937 18.368 20.421
67 -38.766 9.590 21.648
68 -30.896 27.047 16.453
69 -4.432 -10.562 10.061
70 -4.290 33.170 22.942
71 7.285 41.416 23.906
72 24.411 40.531 23.584
73 45.409 -32.420 20.831
74 49.341 -34.047 15.269
75 -7.730 -47.724 21.692
76 -10.563 -29.082 17.984
77 4.412 -41.182 16.845
78 31.822 -37.297 19.665
79 -43.355 31.093 17.688
80 -44.353 -44.723 13.832
81 -16.961 38.438 20.715
82 -21.225 -39.244 18.156
83 -42.022 -8.686 20.362
84 -42.904 -25.498 18.394
85 43.822 -25.990 21.287
86 43.013 -9.071 19.285
87 -36.901 -24.185 21.938
88 -28.251 -36.583 19.330
89 -19.830 -22.412 21.677
90 -3.789 -15.663 17.439
91 40.453 -21.796 17.432
92 -40.778 -31.188 18.762
93 -27.072 -48.609 18.913
94 -18.035 -1.791 19.909
95 -20.781 -7.912 22.563
96 47.307 -15.432 19.101
97 30.700 5.097 22.801
98 46.453 0.171 17.810
99 -27.439 -5.860 22.626
100 -30.526 -18.007 23.219
101 -18.280 -15.187 25.302
102 -18.367 6.044 18.864
103 41.265 -1.686 22.743
104 29.227 -14.814 19.196
105 -36.080 -32.715 18.930
106 7.475 7.061 25.002
107 -18.586 -45.207 21.864
108 35.227 11.148 21.388
109 -7.581 38.773 22.048
110 -43.685 14.083 22.037
111 -29.533 39.735 17.613
112 8.760 -39.400 22.421
113 -14.962 24.624 12.030
114 18.627 -32.888 23.036
115 -31.300 33.612 15.608
116 -38.024 45.839 16.567
117 -15.104 36.893 18.162
118 -12.809 -23.029 21.589
119 -21.614 36.264 16.680
120 42.917 -36.838 18.738
121 6.104 -14.961 14.468
122 44.032 -41.556 17.618
123 -24.493 21.886 17.366
124 -24.361 29.941 14.374
125 -25.060 43.383 16.437
126 -6.017 -24.640 19.207
127 -32.617 -40.549 18.059
128 -43.285 -43.364 18.827
129 -29.856 -46.089 16.881
130 -16.547 -43.619 22.547
131 -16.257 42.814 18.932
132 -9.236 -11.694 14.455
133 13.488 -35.422 24.436
134 -47.456 -32.714 18.123
135 39.476 -28.008 16.087
136 -21.933 -43.522 15.390
137 -17.347 -38.250 16.738
138 -4.948 -39.747 21.598
139 -31.018 -28.912 21.332
140 -36.364 30.461 17.542
141 -39.639 18.272 23.663
142 -24.162 -13.582 19.136
143 -8.935 -32.699 22.108
144 0.001 -19.219 17.888
145 -6.912 -24.885 20.683
146 7.785 -31.229 15.972
147 22.176 -7.478 21.335
148 8.755 -13.323 20.831
149 44.081 41.160 11.938
150 -8.451 -37.721 17.465
151 18.671 -2.776 23.374
152 12.668 -26.749 18.071
153 1.582 -21.252 20.750
154 20.832 -27.718 16.190
155 44.220 -45.690 12.598
156 -0.226 -37.737 17.634
157 -25.130 -19.197 23.170
158 2.086 -31.271 18.180
159 -20.445 -33.083 19.984
160 23.801 1.116 24.230
161 18.283 -17.922 20.256
162 -38.985 -13.770 20.702
163 -26.264 -27.413 20.276
164 10.396 -19.375 20.415
165 -16.343 -22.847 16.516
166 29.992 -8.215 21.661
167 35.052 -19.475 16.953
168 3.052 -6.800 22.509
169 -10.350 -5.413 19.222
170 14.371 -10.383 23.471
171 11.896 -4.191 21.773
172 18.152 8.741 23.669
173 25.748 -47.786 18.578
174 31.613 -0.735 23.898
175 12.660 25.645 23.549
176 2.933 29.345 25.170
177 9.369 18.791 26.817
178 15.805 4.798 27.866
179 27.556 -25.571 14.796
180 -5.112 -7.835 21.201
181 -30.571 3.471 20.496
182 19.816 -22.114 21.210
183 2.826 47.437 22.911
184 25.488 -33.064 21.442
185 44.826 42.162 22.994
186 25.208 -48.487 25.325
187 14.635 -17.430 17.083
188 -1.901 -33.370 22.163
189 12.306 -47.265 20.052
190 42.552 35.750 23.213
191 37.318 -46.069 22.599
192 4.725 -22.289 21.600
193 -40.815 -37.793 17.371
194 11.890 -12.862 14.286
195 35.251 -31.746 17.816
196 27.121 -27.638 19.677
197 36.024 -39.105 20.202
198 -47.119 41.940 17.526
199 0.837 -40.694 23.063
200 23.797 -39.795 20.198
201 -42.859 -21.372 23.554
202 39.407 -20.211 21.246
203 25.782 -18.892 20.423
204 34.529 -9.576 20.411
205 44.397 -13.247 23.180
206 5.534 6.856 14.248
207 31.598 -18.085 22.350
208 7.250 -0.481 15.453
209 -43.458 -15.204 23.193
210 -38.296 -31.524 21.776
211 4.276 -3.483 12.145
212 25.757 -11.708 22.360
213 15.634 37.478 24.624
214 -43.669 -3.197 20.742
215 45.381 6.365 21.351
216 -38.755 -6.877 20.879
217 -6.925 3.994 21.120
218 8.059 12.831 26.032
219 3.572 22.105 26.920
220 16.042 30.267 21.039
221 26.629 13.042 23.633
222 -12.126 -0.151 21.261
223 -11.981 24.600 19.236
224 29.480 28.362 21.838
225 -2.500 22.858 23.177
226 -41.163 19.863 20.059
227 35.953 27.401 19.101
228 -16.641 13.248 17.984
229 -3.778 14.090 18.943
230 11.643 34.817 21.621
231 34.921 38.666 17.359
232 25.621 22.451 22.866
233 34.936 17.384 19.836
234 40.017 37.599 13.987
235 19.547 33.838 22.575
236 11.197 39.977 19.347
237 16.972 -33.927 14.205
238 22.938 38.064 20.351
239 40.234 18.672 23.030
240 -0.846 42.320 18.383
241 -11.437 18.284 16.502
242 19.552 43.222 21.370
243 13.925 -46.486 18.917
244 41.709 -39.559 16.143
245 19.014 -44.563 17.796
246 32.260 33.114 18.402
247 -4.693 29.228 18.622
248 21.765 -38.452 15.147
249 39.157 -31.135 19.800
250 32.638 46.241 18.943
251 2.797 10.089 21.330
252 8.256 46.910 18.834
253 38.634 -2.429 20.413
254 28.642 2.763 19.580
255 0.456 1.422 7.452
256 3.050 11.792 14.196
257 24.736 14.532 17.886
258 16.787 -10.155 18.607
259 12.676 11.651 18.656
260 13.184 1.081 15.385
261 27.365 26.576 25.486
262 -7.878 -18.191 14.547
263 -42.112 32.576 20.865
264 15.069 21.684 17.986
265 33.045 27.166 25.252
266 21.810 -0.186 19.477
267 18.227 26.690 20.415
268 33.759 18.366 21.255
269 39.491 13.272 23.036
270 30.662 9.368 20.192
271 5.470 35.303 22.685
272 21.663 -44.343 20.999
273 31.261 33.178 24.335
274 21.854 22.665 20.876
275 21.853 7.932 18.588
276 -40.168 3.682 19.642
277 -42.292 23.997 22.199
278 10.233 28.731 21.263
279 17.745 41.831 19.536
280 38.406 25.165 26.534
281 -49.329 -0.465 20.887
282 40.398 -8.120 21.362
283 -2.531 46.118 22.933
284 7.959 -30.856 20.497
285 -34.467 -23.724 22.206
286 30.541 44.284 25.878
287 45.682 29.897 21.964
288 -22.251 -0.089 20.756
289 21.484 16.532 23.513
290 46.912 10.195 21.908
291 35.320 -13.352 16.102
292 -30.431 14.048 17.362
293 -8.976 -17.325 21.645
294 -32.661 2.301 16.805
295 49.317 -5.509 17.711
296 -37.756 4.459 16.054
297 41.445 6.158 21.442
298 -33.148 -3.499 19.543
299 -10.065 12.238 16.649
300 -48.323 17.153 20.974
301 45.010 28.147 17.838
302 -39.630 29.183 13.254
303 -45.191 5.065 18.214
304 -35.936 11.953 16.540
305 -19.816 19.624 18.279
306 -32.055 29.757 15.358
307 -41.533 36.169 10.005
308 -35.448 20.960 16.720
309 -23.384 0.511 20.005
310 -25.101 40.569 18.180
311 -0.547 -45.779 17.603
312 -35.291 32.643 9.548
313 -25.109 20.826 17.494
314 -26.202 27.012 18.678
315 -34.805 43.850 14.006
316 -18.819 30.611 20.309
317 13.019 -40.248 16.874
318 -0.655 37.112 20.924
319 -34.142 -41.553 15.237
320 -39.509 43.886 12.464
321 -9.491 38.639 18.839
322 -12.164 31.977 17.598
323 3.437 -1.596 20.318
324 -45.713 -52.599 9.918
325 -2.062 -15.946 12.847
326 -27.435 -37.600 18.257
327 -18.094 -29.624 18.791
328 -22.647 -26.123 18.746
329 -16.775 -15.505 19.204
330 -2.628 9.599 11.219
331 -15.718 -1.797 19.491
332 -29.476 -20.107 17.485
333 -10.618 -4.938 12.227
334 1.423 17.458 14.706
335 -4.503 16.630 14.718
336 32.450 -2.029 21.591
337 20.529 35.464 21.630
338 -19.348 -7.844 19.464
339 -26.760 -6.856 21.422
340 -4.539 4.393 11.819
341 -5.741 1.934 7.121
342 4.781 21.919 18.908
343 -19.797 6.928 20.928
344 -24.555 16.834 19.796
345 -5.664 24.465 18.432
346 -32.891 -6.571 18.691
347 2.354 28.462 19.825
348 18.058 16.251 16.335
349 -9.603 28.582 17.743
350 -31.282 11.454 22.342
351 -17.580 16.428 18.401
352 -13.884 6.206 17.270
353 -13.631 13.767 11.761
354 9.712 18.008 18.896
355 -37.987 9.024 21.309
356 -29.969 27.506 16.964
357 -4.248 -10.813 9.284
358 -5.755 32.673 22.541
359 6.675 41.952 24.227
360 24.564 41.173 23.241
361 45.314 -32.299 20.778
362 -45.890 -33.510 16.314
363 -8.277 -47.943 21.573
364 -11.044 -29.464 17.708
365 3.972 -41.396 17.411
366 31.776 -36.643 19.998
367 -43.072 31.311 17.828
368 -45.805 -43.071 14.477
369 -15.628 39.837 19.709
370 -21.129 -39.101 18.814
371 -41.628 -8.980 19.850
372 -42.244 -23.659 18.856
373 44.149 -25.710 21.099
374 42.623 -9.185 20.147
375 -35.949 -23.979 22.255
376 -28.512 -36.367 19.378
377 -19.827 -21.781 21.621
378 -3.429 -15.706 18.677
379 39.741 -20.721 18.670
380 -41.663 -29.499 19.260
381 -26.931 -48.467 18.185
382 -17.571 -1.467 19.770
383 -20.039 -7.591 22.737
384 46.370 -14.790 19.922
385 30.710 4.167 22.987
386 46.755 0.417 18.088
387 -27.293 -4.398 22.168
388 -30.364 -17.573 23.869
389 -16.870 -14.893 25.817
390 -18.152 6.546 18.392
391 40.134 0.160 23.661
392 28.179 -14.323 19.301
393 -35.907 -32.647 19.306
394 8.486 7.101 24.551
395 -17.155 -45.435 22.745
396 34.226 10.748 19.773
397 -7.760 38.754 22.211
398 -42.899 13.804 22.628
399 -29.972 40.435 17.784
400 8.764 -39.195 22.070
401 -15.624 25.585 12.291
402 18.620 -33.314 23.282
403 -30.436 34.219 15.102
404 -37.665 44.955 15.257
405 -15.861 37.488 18.956
406 -13.375 -22.408 20.312
407 -20.972 36.906 17.387
408 43.162 -35.948 19.695
409 6.639 -15.783 14.608
410 44.186 -41.037 17.398
411 -23.917 22.236 18.702
412 -23.957 30.033 14.725
413 -25.056 43.824 15.489
414 -6.795 -24.375 18.537
415 -33.485 -40.651 17.538
416 -43.186 -43.071 17.481
417 -30.325 -46.122 16.440
418 -17.489 -43.551 22.006
419 -16.376 43.928 18.992
420 -9.076 -10.921 14.131
421 13.704 -36.352 23.812
422 -47.302 -31.918 18.719
423 39.459 -27.814 15.558
424 -22.509 -42.660 14.366
425 -17.920 -37.614 16.572
426 -5.780 -39.212 21.667
427 -30.519 -28.942 21.931
428 -35.937 31.435 17.106
429 -38.680 18.435 23.342
430 -24.796 -13.279 18.543
431 -9.283 -32.388 21.895
432 0.493 -19.505 17.276
433 -7.046 -25.243 20.741
434 7.884 -32.006 16.727
435 22.451 -7.834 21.082
436 8.379 -13.690 22.002
437 43.730 41.697 11.894
438 -9.040 -38.086 17.500
439 18.831 -2.759 23.252
440 12.732 -27.410 18.948
441 0.739 -21.091 21.354
442 20.339 -27.959 16.514
443 44.688 -46.449 12.356
444 -0.402 -36.951 17.891
445 -24.790 -18.139 23.337
446 2.173 -30.577 18.023
447 -18.995 -33.799 20.730
448 23.372 0.223 24.855
449 17.835 -17.372 19.878
450 -38.915 -13.815 20.923
451 -26.241 -27.800 19.877
452 11.074 -18.156 19.249
453 -16.478 -22.928 16.386
454 29.646 -8.349 21.115
455 33.910 -20.809 16.629
456 3.306 -6.830 22.059
457 -10.512 -5.322 19.876
458 14.024 -10.406 23.456
459 12.365 -3.699 21.818
460 18.186 8.532 23.951
461 25.140 -47.653 18.592
462 32.288 -2.117 23.423
463 10.836 24.937 23.310
464 4.531 28.913 25.238
465 9.944 18.397 26.661
466 16.274 4.852 27.837
467 27.316 -26.007 15.934
468 -4.508 -8.010 20.906
469 -29.858 2.412 19.958
470 20.376 -21.957 21.306
471 2.077 47.431 23.248
472 25.777 -33.367 21.695
473 44.854 42.801 22.904
474 25.356 -48.833 25.402
475 15.322 -16.926 17.318
476 -2.656 -33.400 20.365
477 11.950 -47.390 20.328
478 42.961 36.955 22.919
479 35.726 -45.402 24.272
480 4.675 -21.758 21.780
481 -40.568 -36.931 16.934
482 11.758 -12.859 14.206
483 35.483 -31.760 16.975
484 27.336 -27.577 19.429
485 36.689 -39.218 19.668
486 -46.357 41.618 17.456
487 0.002 -40.589 22.558
488 23.525 -39.918 21.247
489 -43.269 -21.304 22.699
490 40.191 -20.594 21.145
491 25.728 -18.024 20.298
492 34.964 -10.441 20.189
493 43.627 -13.279 23.038
494 5.766 6.876 14.077
495 32.432 -18.172 21.848
496 7.087 -1.122 15.098
497 -44.110 -14.034 23.080
498 -39.474 -31.289 22.312
499 4.118 -4.077 11.067
500 26.597 -11.667 22.641
so, using these commands I can find the intervals, as below
x.bin <- seq(floor(min(d[,1])), ceiling(max(df[,1])), by=2)
y.bin <- seq(floor(min(d[,2])), ceiling(max(df[,2])), by=2)
> x.bin
[1] -50 -48 -46 -44 -42 -40 -38 -36 -34 -32 -30 -28 -26 -24 -22 -20 -18 -16 -14
[20] -12 -10 -8 -6 -4 -2 0 2 4 6 8 10 12 14 16 18 20 22 24
[39] 26 28 30 32 34 36 38 40 42 44 46 48 50
> y.bin
[1] -53 -51 -49 -47 -45 -43 -41 -39 -37 -35 -33 -31 -29 -27 -25 -23 -21 -19 -17
[20] -15 -13 -11 -9 -7 -5 -3 -1 1 3 5 7 9 11 13 15 17 19 21
[39] 23 25 27 29 31 33 35 37 39 41 43 45 47
But, then I don't know how to assign each row of the raw data (df) to each x.bin and y.bin and calculate the aggregate (sum) of each bin.
library(plyr)
#I am using cut function with 50 breaks for both v1 and v2 and ddply from plyr package for computing the mean
newdata<-ddply(df,.(cut(v1,50),cut(v2,50)),summarise,mean.v3=mean(v3))
> head(newdata)
cut(v1, 50) cut(v2, 50) mean.v3
1 (-49.4,-47.5] (-34.7,-32.7] 18.123
2 (-49.4,-47.5] (-0.576,1.43] 20.887
3 (-49.4,-47.5] (15.5,17.5] 20.887
4 (-47.5,-45.5] (-52.7,-50.7] 9.918
5 (-47.5,-45.5] (-44.7,-42.7] 14.477
6 (-47.5,-45.5] (-34.7,-32.7] 16.314
Updated as per the comments: If you want the lower, middle and mid-points, you can use the following function or use with details as follow(you need to use the sub function to deal with ( and ]):
df$newv1<-with(df,cut(v1,50))
df$newv2<-with(df,cut(v2,50))
df$lowerv1<-with(df,as.numeric( sub("\\((.+),.*", "\\1", newv1))) #lower value
df$upperv1<-with(df,as.numeric( sub("[^,]*,([^]]*)\\]", "\\1", newv1))) # upper value
df$midv1<-with(df,(lowerv1+upperv1)/2) #mid value
df$lowerv2<-with(df,as.numeric( sub("\\((.+),.*", "\\1",newv2))) #lower value
df$upperv2<-with(df,as.numeric( sub("[^,]*,([^]]*)\\]", "\\1", newv2))) # upper value
df$midv2<-with(df,(lowerv2+upperv2)/2)#mid value
newdata<-ddply(df,.(newv1,newv2),transform,mean.v3=mean(v3))
> head(newdata)
v1 v2 v3 newv1 newv2 lowerv1 upperv1 midv1 lowerv2 upperv2 midv2 mean.v3
1 -47.456 -32.714 18.123 (-49.4,-47.5] (-34.7,-32.7] -49.4 -47.5 -48.45 -34.700 -32.70 -33.700 18.123
2 -49.329 -0.465 20.887 (-49.4,-47.5] (-0.576,1.43] -49.4 -47.5 -48.45 -0.576 1.43 0.427 20.887
3 -48.652 16.558 20.800 (-49.4,-47.5] (15.5,17.5] -49.4 -47.5 -48.45 15.500 17.50 16.500 20.887
4 -48.323 17.153 20.974 (-49.4,-47.5] (15.5,17.5] -49.4 -47.5 -48.45 15.500 17.50 16.500 20.887
5 -45.713 -52.599 9.918 (-47.5,-45.5] (-52.7,-50.7] -47.5 -45.5 -46.50 -52.700 -50.70 -51.700 9.918
6 -45.805 -43.071 14.477 (-47.5,-45.5] (-44.7,-42.7] -47.5 -45.5 -46.50 -44.700 -42.70 -43.700 14.477

Iteratively compute drift coefficient from random walk with drift function in R, compile into list

My objective is to list the drift coefficient from a random walk with drift forecast function, applied to a set of historical data (below). Specifically I am trying to gather the drift coefficient starting from the random walk with drift model of the first year, then cumulatively to the last, recording the coefficient each time, meaning iteratively or each additional year (recording this into a list? if that is appropriate). To be clear each new random walk forecast is including all the previous years.
The data is a list of 241 consumption levels, and I am attempting to discern how the drift coefficent would change over the course of iteratively progressing from n=1 to n=241
Where for example the random walk with drift model is Y[t] = c + Y[t-1] + Z[t] where Z[t] is a normal error and c is the coefficient i am looking for. My current attempts at this involve a for loop function and extracting the c coefficient from the rwf() function from the "Forecast" package in R.
To extract this, I am doing as such
rwf(x, h = 1, drift = TRUE)$model[[1]]
which extracts the drift coefficient.
The problem is, my attempts at subsetting the data within the rwf call have failed, and I also don't believe, through trial and error and research, that rwf() supports the subset argument, as an lm model does for example. In this sense my attempts at looping the function have also failed.
An example of such code is
for (i in 1:5){print((rwf(x[1:i], h = 1, drift = TRUE))$model[[1]])}
which gives me the following error
Error in lm.fit(x, y, offset = offset, singular.ok = singular.ok, ...) :
0 (non-NA) cases
In addition: Warning message:
In is.na(rows) : is.na() applied to non-(list or vector) of type 'NULL'
Any help would be much appreciated.
I read SO a lot for help but this is my first time asking a question.
The data is as follows
PCE
1 1306.7
2 1309.6
3 1335.3
4 1341.8
5 1389.2
6 1405.7
7 1414.2
8 1411.0
9 1401.6
10 1406.7
11 1425.0
12 1444.4
13 1474.7
14 1507.8
15 1536.6
16 1555.6
17 1575.2
18 1577.8
19 1583.0
20 1586.6
21 1608.4
22 1619.5
23 1622.4
24 1635.3
25 1636.1
26 1613.9
27 1627.1
28 1653.8
29 1675.6
30 1706.7
31 1732.9
32 1751.0
33 1752.9
34 1769.7
35 1792.1
36 1785.0
37 1787.4
38 1786.9
39 1813.4
40 1822.2
41 1858.7
42 1878.5
43 1901.6
44 1917.0
45 1944.2
46 1957.3
47 1976.0
48 2002.9
49 2019.6
50 2059.5
51 2095.8
52 2134.3
53 2140.2
54 2187.8
55 2212.0
56 2250.0
57 2313.2
58 2347.4
59 2353.5
60 2380.4
61 2390.3
62 2404.2
63 2437.0
64 2449.5
65 2464.6
66 2523.4
67 2562.1
68 2610.3
69 2622.3
70 2651.7
71 2668.6
72 2681.5
73 2702.9
74 2719.5
75 2731.9
76 2755.9
77 2748.4
78 2800.9
79 2826.6
80 2849.1
81 2896.5
82 2935.2
83 2991.2
84 3037.4
85 3108.6
86 3165.5
87 3163.9
88 3175.3
89 3166.0
90 3138.3
91 3149.2
92 3162.2
93 3115.8
94 3142.0
95 3194.4
96 3239.9
97 3274.2
98 3339.6
99 3370.3
100 3405.9
101 3450.3
102 3489.7
103 3509.0
104 3542.5
105 3595.9
106 3616.9
107 3694.2
108 3709.7
109 3739.6
110 3758.5
111 3756.3
112 3793.2
113 3803.3
114 3796.7
115 3710.5
116 3750.3
117 3800.3
118 3821.1
119 3821.1
120 3836.6
121 3807.6
122 3832.2
123 3845.9
124 3875.4
125 3946.1
126 3984.8
127 4063.9
128 4135.7
129 4201.3
130 4237.3
131 4297.9
132 4331.1
133 4388.1
134 4462.5
135 4503.2
136 4588.7
137 4598.8
138 4637.2
139 4686.6
140 4768.5
141 4797.2
142 4789.9
143 4854.0
144 4908.2
145 4920.0
146 5002.2
147 5038.5
148 5078.3
149 5138.1
150 5156.9
151 5180.0
152 5233.7
153 5259.3
154 5300.9
155 5318.4
156 5338.6
157 5297.0
158 5282.0
159 5322.2
160 5342.6
161 5340.2
162 5432.0
163 5464.2
164 5524.6
165 5592.0
166 5614.7
167 5668.6
168 5730.1
169 5781.1
170 5845.5
171 5888.8
172 5936.0
173 5994.6
174 6001.6
175 6050.8
176 6104.9
177 6147.8
178 6204.0
179 6274.2
180 6311.8
181 6363.2
182 6427.3
183 6453.3
184 6563.0
185 6638.1
186 6704.1
187 6819.5
188 6909.9
189 7015.9
190 7085.1
191 7196.6
192 7283.1
193 7385.8
194 7497.8
195 7568.3
196 7642.4
197 7710.0
198 7740.8
199 7770.0
200 7804.2
201 7926.4
202 7953.7
203 7994.1
204 8048.3
205 8076.9
206 8117.7
207 8198.1
208 8308.5
209 8353.7
210 8427.6
211 8465.1
212 8539.1
213 8631.3
214 8700.1
215 8786.2
216 8852.9
217 8874.9
218 8965.8
219 9019.8
220 9073.9
221 9158.3
222 9209.2
223 9244.5
224 9285.2
225 9312.6
226 9289.1
227 9285.8
228 9196.0
229 9076.0
230 9040.9
231 8998.5
232 9050.3
233 9060.2
234 9121.2
235 9186.9
236 9247.1
237 9328.4
238 9376.7
239 9392.7
240 9433.5
241 9482.1
You need at least two points to fit your model. Here's how I'd approach the problem after reading your data into a data.frame named x:
library(forecast)
drifts <- sapply(2:nrow(x), function(zz) rwf(x[1:zz,], drift = TRUE)$model$drift)
I'm not sure if this is what you were expecting or not, but here's a plot of your drift values:

Resources