I'm trying to impute NA
's of temperature data in R. It is spatiotemporal data which has 487 observatories and 60 time units (60 months).
What I want to do here is replace NA
with the value of which has the smallest distance (not zero) from the NA
's observatory in the same month.
Here is my R code (temp_1 is the name of my data).
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
}
}
However, when I run this I get an error message
Error in temp_1[i, ][is.na(temp_1[i, ])][j] = new : replacement has length zero
I typed class(new)
and it says data.frame, so I've changed it into numeric by new=as.numeric(temp_1[i,dz.inex])
. But it comes to the same error.
I don't understand why I get this error message... I appreciate your help a lot.
Consider sapply()
to traverse the columns and maintain the two-dimensionsal structure. Below example dataset demonstrates of converting NAs to nonzero minimums of corresponding column:
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