处理除以0和NA

时间:2017-02-24 04:39:21

标签: r

我有下面的代码,我试图解决这个问题,我的SampleDf $ FV_tsValidation每隔一段时间就有0,这给了我一个Sample SampleDf $ RelError。所以我尝试用NA替换所有的Inf,这很好用,除非有时候我的RelError中也有NA。然后我得到下面的错误。任何提示都非常感谢。

Error:

Error in `[<-.data.frame`(`*tmp*`, SampleDf$RelError == Inf, "RelError",  : 

在数据帧的下标分配中不允许缺少值

Code:

SampleDf$AbsError<-abs(SampleDf$`FV_tsValidation`-SampleDf$`Fcast4append`) ## Absolute Error

SampleDf$RelError<-(SampleDf$AbsError)/(SampleDf$`FV_tsValidation`) ## Relative Error

SampleDf[SampleDf$RelError == Inf,"RelError"]<-NA  ## Fixes dividing by zero issue



Data:

dput(SampleDf)
structure(list(FV_tsValidation = c(NA, 19.2635958655791, NA, 
8.25582679953392, 27.5194226651131, 16.5116535990678, 26.1434515318574, 
8.25582679953392, 6.87985566627827, 4.12791339976696, 5.50388453302261, 
5.50388453302261, 15.1356824658122, 8.25582679953392, 5.50388453302261, 
8.25582679953392, 19.2635958655791, 11.0077690660452, 13.7597113325565, 
16.5116535990678, 9.63179793278957, 2.75194226651131, 19.2635958655791, 
8.25582679953392, 15.1356824658122, 5.50388453302261, 8.25582679953392, 
19.2635958655791, 27.5194226651131, 27.5194226651131, 8.25582679953392, 
8.25582679953392, 16.5116535990678, 5.50388453302261, 8.25582679953392, 
4.12791339976696, 8.25582679953392, 12.3837401993009, 16.5116535990678, 
16.5116535990678, 20.6395669988348, 19.2635958655791, 24.7674803986018, 
8.25582679953392, 27.5194226651131, 16.5116535990678, 16.5116535990678, 
9.63179793278957, NA, 5.50388453302261, 5.50388453302261, 0, 
11.0077690660452, 17.8876247323235, 13.7597113325565, 27.5194226651131, 
11.0077690660452, 4.12791339976696, 2.75194226651131, 8.25582679953392, 
5.50388453302261, 19.2635958655791, 16.5116535990678, 27.5194226651131, 
8.25582679953392, 16.5116535990678, 19.2635958655791, 16.5116535990678, 
15.1356824658122, 11.0077690660452, 6.87985566627827, 13.7597113325565, 
12.3837401993009, 11.0077690660452, 16.5116535990678, 5.50388453302261, 
11.0077690660452, 2.75194226651131, 2.75194226651131, 15.1356824658122, 
15.1356824658122, 2.75194226651131, 15.1356824658122, 2.75194226651131, 
5.50388453302261, 12.3837401993009, 9.63179793278957, 27.5194226651131, 
19.2635958655791, 15.1356824658122, 27.5194226651131, 11.0077690660452, 
27.5194226651131, 13.7597113325565, NA, NA), Fcast4append = c(16.1248963141908, 
15.7649569710559, 15.7850972252287, 15.4966295469409, 15.7089705778812, 
14.3525795773542, 15.2209382064842, 15.1748068927087, 15.2312815242986, 
15.2561693137243, 15.3494952989283, 15.371159864609, 15.5615810349067, 
15.6375522284087, 15.6898102516281, 15.7103224525916, 15.7321447684739, 
15.7539961142042, 15.7868207332038, 15.8050250476546, 15.8148013727396, 
15.801705238544, 15.752786804758, 15.6663858921686, 10.4441309347487, 
9.81254522598927, 9.31964441969533, 8.87672388120499, 10.1479684991817, 
9.31321121239747, 9.4881366927508, 9.39448504134324, 9.41274754878487, 
9.46874829073592, 9.63624315546236, 9.63619084618947, 9.71684851571254, 
9.75536791321517, 9.78332578199983, 9.79172249475212, 9.7988675017234, 
9.79399808659804, 9.79690672901256, 9.79562938473654, 9.79181271420066, 
9.77607637028486, 9.74088813022896, 9.68531640013341, 15.1612272091769, 
14.0204625333526, 15.3518802776299, 14.2070168524094, 14.0713233290003, 
13.6170922552749, 14.1971131400518, 14.1283925448698, 14.2600521560074, 
14.2237007855624, 14.3329967329805, 14.4477116444185, 14.6376309909662, 
14.7438117978197, 14.830991921684, 14.8783305133281, 14.9329150862635, 
14.9862296382146, 15.0366129990685, 15.0698927843637, 15.089537219968, 
15.0771333095779, 15.0176554812956, 14.9105575228617, 12.6753479426371, 
12.2049126032233, 11.6172666348388, 11.1989936141249, 11.0096210077693, 
11.6304177488175, 11.4686956954359, 11.3989967487427, 11.3565584073783, 
11.395540477007, 11.4839863357122, 11.6196077947903, 11.6763715111987, 
11.7286463306136, 11.7622413051796, 11.7884088356107, 11.8139535414116, 
11.8421274974364, 11.8630049803503, 11.8829047922484, 11.8972030765912, 
11.894780613094, 11.8630550130325, 11.8009153268958)), .Names = c("FV_tsValidation", 
"Fcast4append"), row.names = c(NA, -96L), class = "data.frame")

0 个答案:

没有答案