R IDW – How to Fill the Gap Using Inverse Distance Weighting Method in R

fillinggapinverse-distance-weightedr

I'm beginner of R. I'm trying to fill the gap using idw, but I still had no idea about this.

The data in used is below:

enter image description here

The names sequence of columns are date/SO2/CO/O3/NO2/PM10/Lat/Lon. As you see, the gap values is existed in NO2 column. So I totally want to fill up this gap using idw through all location at the same date. It means like below picture num2.

enter image description here

Above picture has same date and different locations. So I want to use it to fill the gap by using idw.

I hope that you can understand what I'm trying to say (I'm Korean).

EDIT : Show my terrible code below

library(ggplot2)
library(rgdal)
library(dplyr)
library(tidyr)
library(gstat)
library(rgdal)
library(raster)
library(rgeos)
library(scales)
library(GISTools)
library(rgdal)
library(sp)
air_test <- read.csv("D:/aircondition/air_1Qu.csv", header = T)
str(air_test)
air_order <- air_test[order(air_test$측정일시),]
air_order <- air_order[,4:11]
a.fac <- as.factor(air_order$측정일시)
a.df <- data.frame(a.fac,air_order)
a.df <- a.df[,-2]
str(a.df)
names(a.df)[1] <-c("DATE") 
a.df$ID <- as.integer(a.df$DATE)
str(a.df)
a.loc <- a.df[,7:9]
colSums(is.na(a.PM10))
str(a.df)



for (i in 1 : length(unique(a.df$ID))) {
  idw <- a.df[a.df$ID==i,]
    for (j in 2:6) {
    idw.air <- na.omit(idw)
    idw.air <- data.frame(idw.air[j], idw.air[,7:9])
    coordinates(idw.air) <- ~Lon + Lat
    proj4string(idw.air) <- CRS("+init=epsg:4326")
    idw.air_t <- spTransform(idw.air, CRS("+init=epsg:32651"))
    idw.final <- idw(formula = names(idw)[j] ~ 1, locations = idw.air_t,
                                      newdata = int.layer, idp = 2)
    idw.output.df <- as.data.frame(idw.final) %>% as.tibble()
    idw.output.df
  }
}

As you see, my codes are very mash. I really hope that the gap in my data is filled and I want to save the whole data which is fixed.
So, I totally want to be professional about R. Could you recommend something like books about R?

ERROR MESSAGES when I ran the code

Error in predict.gstat(g, newdata = newdata, block = block, nsim = nsim,  : 
  too many spatial dimensions: 226
In addition: Warning message: 
In predict.gstat(g, newdata = newdata, block = block, nsim = nsim,  :
  NAs introduced by coercion

Best Answer

Here's how to use idw to predict at some locations.

Use only these two packages:

> library(sp)
> library(gstat)

Make a test data set of 20 points with 20 N values that are the measurement we are interested in:

> d = data.frame(x=runif(20), y=runif(20), N=rnorm(20))
> coordinates(d)=~x+y

Let's set 3 of our measurements to NA because these are missing - we want to fill these locations in:

> d$N[c(5,6,7)]=NA

Now run idw with our valid data as locations and our prediction locations as newdata. Note how we use valid as a true/false index to subset our data:

> valid = !is.na(d$N)
> predictions = idw(N~1, 
       locations=d[valid,,drop=FALSE],
       newdata=d[!valid,,drop=FALSE])

Giving:

> predictions
             coordinates  var1.pred var1.var
5 (0.876017, 0.07675017)  0.3292105       NA
6 (0.3606106, 0.3929375) -0.8818549       NA
7 (0.8988331, 0.8903288)  0.1999656       NA

Note the prediction variance is NA because the idw method has no uncertainty measure. You will get different answers because I've used random numbers.

To put those prediction values back into the original data frame:

> d$N[!valid] = predictions$var1.pred

To get this to work on your data you will still have to convert the projection, but you only have to do this once - I really don't understand why you have a doubly-nested loop in your code. One call to idw will predict at any number of newdata locations given a set of valid location data with coordinates and measurements.