'替换的长度为零' R中的错误

时间:2016-09-17 01:02:32

标签: r na imputation

我试图将NA的温度数据归入R中。它是时空数据,有487个天文台和60个时间单位(60个月)。 我想在这里做的是替换NA,其值在同一个月内与NA天文台的距离最小(非零)。

这是我的R代码(temp_1是我的数据的名称)。

pos.min = function(v){        # find positive minimum index
  v.na = v
  v.na[v==0] = NA
  return(which.min(v.na))
}


for (i in 1:60){
 for (j in 1:sum(is.na(temp_1[i,]))){
   na.index=which(is.na(temp_1[i,]))
   dz.index=pos.min(dz[na.index[j],])
   new=temp_1[i,dz.index]
   temp_1[i,][is.na(temp_1[i,])][j]=new
   }
}

然而,当我运行这个时,我收到一条错误消息

  

temp_1 [i,]中的错误[is.na(temp_1 [i,]]] [j] =新:替换有   零长度

我输入了class(new),它说的是data.frame,因此我已将其new=as.numeric(temp_1[i,dz.inex])更改为数字。但它也出现了同样的错误。

我不明白为什么会收到此错误消息... 非常感谢你的帮助。

1 个答案:

答案 0 :(得分:0)

考虑sapply()遍历列并保持二维结构。下面的示例数据集演示了将NA转换为相应列的非零最小值:

df <- read.table(text="OBS  MONTH1  MONTH2  MONTH3  MONTH4  MONTH5  MONTH6
              1 0.306001774     0.086538253 0.9847485   0.920696749 0.806839772 0.693047683
              2  0.795098073             NA 0.08102032  0.473177189 0.852177898          NA
              3  0.205973354    0.902099959 0.914812457          NA 0.608290972 0.378916134
              4           NA    0.000372107 0.350874402 0.915298814 0.817865272 0.225742663
              5  0.478680167    0.812487579 0.630341993 0.235519315 0.694856788 0.181300605
              6  0.913115578    0.018699114 0.104682383 0.871933902 0.051088907 0.731334073
              7  0.639176591    0.177650025 0.180534357          NA 0.296920889 0.869592176
              8  0.458452966    0.439206666          NA 0.887944511 0.071936749 0.304492684
              9  0.218429871    0.639603625 0.134885823 0.113512933          NA 0.472305502
              10 0.027337984             NA  0.37154713 0.400568794 0.928564041 0.559873876
                 ", header=TRUE)

newdf <- data.frame(sapply(df, function(col) {
     ifelse(is.na(col) & min(col[!is.na(col)]) > 0, min(col[!is.na(col)]), col)
}))
newdf

#    OBS     MONTH1      MONTH2     MONTH3    MONTH4     MONTH5    MONTH6
# 1    1 0.30600177 0.086538253 0.98474850 0.9206967 0.80683977 0.6930477
# 2    2 0.79509807 0.000372107 0.08102032 0.4731772 0.85217790 0.1813006
# 3    3 0.20597335 0.902099959 0.91481246 0.1135129 0.60829097 0.3789161
# 4    4 0.02733798 0.000372107 0.35087440 0.9152988 0.81786527 0.2257427
# 5    5 0.47868017 0.812487579 0.63034199 0.2355193 0.69485679 0.1813006
# 6    6 0.91311558 0.018699114 0.10468238 0.8719339 0.05108891 0.7313341
# 7    7 0.63917659 0.177650025 0.18053436 0.1135129 0.29692089 0.8695922
# 8    8 0.45845297 0.439206666 0.08102032 0.8879445 0.07193675 0.3044927
# 9    9 0.21842987 0.639603625 0.13488582 0.1135129 0.05108891 0.4723055
# 10  10 0.02733798 0.000372107 0.37154713 0.4005688 0.92856404 0.5598739
相关问题