在FX数据之间插值NA值

时间:2018-11-06 20:40:07

标签: r

我有以下数据,但我以某种方式“随机”具有我想估算的NA值。

例如,在这种情况下,我认为仅取NA值之后的结果与之前的结果之间的平均值即可。

 date         eur_usd
2008-01-18 - 1.4616
2008-01-21 - NA
2008-01-22 - 1.4609

NA的值插入为(1.4616 + 14609) / 2 = 1.46125

如果您对估算FX数据有更好的建议,那也将是很好的选择。

数据:

df <- structure(list(WEEK = structure(c(1199660400, 1199746800, 1199833200, 
    1199919600, 1200006000, 1200265200, 1200351600, 1200438000, 1200524400, 
    1200610800, 1200870000, 1200956400, 1201042800, 1201129200, 1201215600, 
    1201474800, 1201561200, 1201647600, 1201734000, 1201820400, 1202079600, 
    1202166000, 1202252400, 1202338800, 1202425200, 1202684400, 1202770800, 
    1202857200, 1202943600, 1203030000, 1203289200, 1203375600, 1203462000, 
    1203548400, 1203634800, 1203894000, 1203980400, 1204066800, 1204153200, 
    1204239600, 1204498800, 1204585200, 1204671600, 1204758000, 1204844400, 
    1205103600, 1205190000, 1205276400, 1205362800, 1205449200, 1205708400, 
    1205794800, 1205881200, 1205967600, 1206054000, 1206313200, 1206399600, 
    1206486000, 1206572400, 1206658800, 1206914400, 1207000800, 1207087200, 
    1207173600, 1207260000, 1207519200, 1207605600, 1207692000, 1207778400, 
    1207864800, 1208124000, 1208210400, 1208296800, 1208383200, 1208469600, 
    1208728800, 1208815200, 1208901600, 1208988000, 1209074400, 1209333600, 
    1209420000, 1209506400, 1209592800, 1209679200, 1209938400, 1210024800, 
    1210111200, 1210197600, 1210284000, 1210543200, 1210629600, 1210716000, 
    1210802400, 1210888800, 1211148000, 1211234400, 1211320800, 1211407200, 
    1211493600), class = c("POSIXct", "POSIXt"), tzone = ""), USD_EUR = c(1.4684, 
    1.4717, 1.4663, 1.474, 1.4787, 1.4877, 1.4842, 1.4643, 1.469, 
    1.4616, NA, 1.4609, 1.4574, 1.4737, 1.4673, 1.4789, 1.4767, 1.4782, 
    1.4841, 1.4851, 1.4832, 1.4638, 1.4647, 1.4495, 1.4505, 1.4506, 
    1.4592, 1.4569, 1.4635, 1.4674, NA, 1.4742, 1.4663, 1.4813, 1.4825, 
    1.4822, 1.4889, 1.5132, 1.5172, 1.5187, 1.5195, 1.521, 1.5282, 
    1.5356, 1.5333, 1.5369, 1.5329, 1.5495, 1.5584, 1.5604, 1.5765, 
    1.5784, 1.5642, 1.5417, 1.5447, 1.5394, 1.5595, 1.5761, 1.5798, 
    1.5759, 1.5805, 1.5615, 1.5618, 1.5667, 1.5735, 1.5713, 1.5705, 
    1.5804, 1.5744, 1.5796, 1.5827, 1.5801, 1.5978, 1.5924, 1.5742, 
    1.5881, 1.601, 1.5898, 1.5668, 1.5634, 1.5648, 1.5604, 1.5568, 
    1.5458, 1.5431, 1.5491, 1.5548, 1.537, 1.5409, 1.5447, 1.5536, 
    1.5509, 1.5467, 1.5492, 1.5591, 1.5516, 1.5667, 1.5772, 1.5703, 
    1.5784)), row.names = c(NA, 100L), class = "data.frame")

2 个答案:

答案 0 :(得分:2)

使用dplyr软件包

library(dplyr)
df$USD_EUR <- ifelse(is.na(df$USD_EUR), (lag(df$USD_EUR)+ lead(df$USD_EUR))/2,df$USD_EUR) 

答案 1 :(得分:1)

这是一种方法

df <- data.frame(df)
n <- nrow(df)
df$nextp <- c(df$USD_EUR[2:n],NA)
df$previous <- c(NA, df$USD_EUR[1:n-1])
df$meanprevnext <- apply(df, 1 , function(x) mean(c(as.numeric(x["previous"]), as.numeric(x["nextp"]))))
df$result <- ifelse(is.na(df$USD_EUR), df$meanprevnext, df$USD_EUR)

          WEEK USD_EUR previous  nextp meanprevnext  result
1   2008-01-07  1.4684       NA 1.4717           NA 1.46840
2   2008-01-08  1.4717   1.4684 1.4663      1.46735 1.47170
3   2008-01-09  1.4663   1.4717 1.4740      1.47285 1.46630
4   2008-01-10  1.4740   1.4663 1.4787      1.47250 1.47400
5   2008-01-11  1.4787   1.4740 1.4877      1.48085 1.47870
6   2008-01-14  1.4877   1.4787 1.4842      1.48145 1.48770
7   2008-01-15  1.4842   1.4877 1.4643      1.47600 1.48420
8   2008-01-16  1.4643   1.4842 1.4690      1.47660 1.46430
9   2008-01-17  1.4690   1.4643 1.4616      1.46295 1.46900
10  2008-01-18  1.4616   1.4690     NA           NA 1.46160
11  2008-01-21      NA   1.4616 1.4609      1.46125 1.46125
12  2008-01-22  1.4609       NA 1.4574           NA 1.46090
13  2008-01-23  1.4574   1.4609 1.4737      1.46730 1.45740
14  2008-01-24  1.4737   1.4574 1.4673      1.46235 1.47370
15  2008-01-25  1.4673   1.4737 1.4789      1.47630 1.46730
16  2008-01-28  1.4789   1.4673 1.4767      1.47200 1.47890
17  2008-01-29  1.4767   1.4789 1.4782      1.47855 1.47670
18  2008-01-30  1.4782   1.4767 1.4841      1.48040 1.47820
19  2008-01-31  1.4841   1.4782 1.4851      1.48165 1.48410
20  2008-02-01  1.4851   1.4841 1.4832      1.48365 1.48510
21  2008-02-04  1.4832   1.4851 1.4638      1.47445 1.48320
22  2008-02-05  1.4638   1.4832 1.4647      1.47395 1.46380
23  2008-02-06  1.4647   1.4638 1.4495      1.45665 1.46470
24  2008-02-07  1.4495   1.4647 1.4505      1.45760 1.44950
25  2008-02-08  1.4505   1.4495 1.4506      1.45005 1.45050
26  2008-02-11  1.4506   1.4505 1.4592      1.45485 1.45060
27  2008-02-12  1.4592   1.4506 1.4569      1.45375 1.45920
28  2008-02-13  1.4569   1.4592 1.4635      1.46135 1.45690
29  2008-02-14  1.4635   1.4569 1.4674      1.46215 1.46350
30  2008-02-15  1.4674   1.4635     NA           NA 1.46740
31  2008-02-18      NA   1.4674 1.4742      1.47080 1.47080
32  2008-02-19  1.4742       NA 1.4663           NA 1.47420
33  2008-02-20  1.4663   1.4742 1.4813      1.47775 1.46630
34  2008-02-21  1.4813   1.4663 1.4825      1.47440 1.48130
35  2008-02-22  1.4825   1.4813 1.4822      1.48175 1.48250
36  2008-02-25  1.4822   1.4825 1.4889      1.48570 1.48220
37  2008-02-26  1.4889   1.4822 1.5132      1.49770 1.48890
38  2008-02-27  1.5132   1.4889 1.5172      1.50305 1.51320
39  2008-02-28  1.5172   1.5132 1.5187      1.51595 1.51720
40  2008-02-29  1.5187   1.5172 1.5195      1.51835 1.51870
41  2008-03-03  1.5195   1.5187 1.5210      1.51985 1.51950
42  2008-03-04  1.5210   1.5195 1.5282      1.52385 1.52100
43  2008-03-05  1.5282   1.5210 1.5356      1.52830 1.52820
44  2008-03-06  1.5356   1.5282 1.5333      1.53075 1.53560
45  2008-03-07  1.5333   1.5356 1.5369      1.53625 1.53330
46  2008-03-10  1.5369   1.5333 1.5329      1.53310 1.53690
47  2008-03-11  1.5329   1.5369 1.5495      1.54320 1.53290
48  2008-03-12  1.5495   1.5329 1.5584      1.54565 1.54950
49  2008-03-13  1.5584   1.5495 1.5604      1.55495 1.55840
50  2008-03-14  1.5604   1.5584 1.5765      1.56745 1.56040
51  2008-03-17  1.5765   1.5604 1.5784      1.56940 1.57650
52  2008-03-18  1.5784   1.5765 1.5642      1.57035 1.57840
53  2008-03-19  1.5642   1.5784 1.5417      1.56005 1.56420
54  2008-03-20  1.5417   1.5642 1.5447      1.55445 1.54170
55  2008-03-21  1.5447   1.5417 1.5394      1.54055 1.54470
56  2008-03-24  1.5394   1.5447 1.5595      1.55210 1.53940
57  2008-03-25  1.5595   1.5394 1.5761      1.55775 1.55950
58  2008-03-26  1.5761   1.5595 1.5798      1.56965 1.57610
59  2008-03-27  1.5798   1.5761 1.5759      1.57600 1.57980
60  2008-03-28  1.5759   1.5798 1.5805      1.58015 1.57590
61  2008-03-31  1.5805   1.5759 1.5615      1.56870 1.58050
62  2008-04-01  1.5615   1.5805 1.5618      1.57115 1.56150
63  2008-04-02  1.5618   1.5615 1.5667      1.56410 1.56180
64  2008-04-03  1.5667   1.5618 1.5735      1.56765 1.56670
65  2008-04-04  1.5735   1.5667 1.5713      1.56900 1.57350
66  2008-04-07  1.5713   1.5735 1.5705      1.57200 1.57130
67  2008-04-08  1.5705   1.5713 1.5804      1.57585 1.57050
68  2008-04-09  1.5804   1.5705 1.5744      1.57245 1.58040
69  2008-04-10  1.5744   1.5804 1.5796      1.58000 1.57440
70  2008-04-11  1.5796   1.5744 1.5827      1.57855 1.57960
71  2008-04-14  1.5827   1.5796 1.5801      1.57985 1.58270
72  2008-04-15  1.5801   1.5827 1.5978      1.59025 1.58010
73  2008-04-16  1.5978   1.5801 1.5924      1.58625 1.59780
74  2008-04-17  1.5924   1.5978 1.5742      1.58600 1.59240
75  2008-04-18  1.5742   1.5924 1.5881      1.59025 1.57420
76  2008-04-21  1.5881   1.5742 1.6010      1.58760 1.58810
77  2008-04-22  1.6010   1.5881 1.5898      1.58895 1.60100
78  2008-04-23  1.5898   1.6010 1.5668      1.58390 1.58980
79  2008-04-24  1.5668   1.5898 1.5634      1.57660 1.56680
80  2008-04-25  1.5634   1.5668 1.5648      1.56580 1.56340
81  2008-04-28  1.5648   1.5634 1.5604      1.56190 1.56480
82  2008-04-29  1.5604   1.5648 1.5568      1.56080 1.56040
83  2008-04-30  1.5568   1.5604 1.5458      1.55310 1.55680
84  2008-05-01  1.5458   1.5568 1.5431      1.54995 1.54580
85  2008-05-02  1.5431   1.5458 1.5491      1.54745 1.54310
86  2008-05-05  1.5491   1.5431 1.5548      1.54895 1.54910
87  2008-05-06  1.5548   1.5491 1.5370      1.54305 1.55480
88  2008-05-07  1.5370   1.5548 1.5409      1.54785 1.53700
89  2008-05-08  1.5409   1.5370 1.5447      1.54085 1.54090
90  2008-05-09  1.5447   1.5409 1.5536      1.54725 1.54470
91  2008-05-12  1.5536   1.5447 1.5509      1.54780 1.55360
92  2008-05-13  1.5509   1.5536 1.5467      1.55015 1.55090
93  2008-05-14  1.5467   1.5509 1.5492      1.55005 1.54670
94  2008-05-15  1.5492   1.5467 1.5591      1.55290 1.54920
95  2008-05-16  1.5591   1.5492 1.5516      1.55040 1.55910
96  2008-05-19  1.5516   1.5591 1.5667      1.56290 1.55160
97  2008-05-20  1.5667   1.5516 1.5772      1.56440 1.56670
98  2008-05-21  1.5772   1.5667 1.5703      1.56850 1.57720
99  2008-05-22  1.5703   1.5772 1.5784      1.57780 1.57030
100 2008-05-23  1.5784   1.5703     NA           NA 1.57840