1

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
4
  • Have you looked at any of the packages that have functions do IDW? e.g. gstat?
    – Spacedman
    Commented Jul 14, 2019 at 12:06
  • Hi! and thank you for your comments. I had used a lots of packages, and I'm totally beginner of R. So I don't know exactly the packages which I used. I used this packages below to run the code. library(dplyr) library(tidyr) library(gstat) library(rgdal) library(raster) library(rgeos) library(scales)
    – CHOI
    Commented Jul 14, 2019 at 12:39
  • Have you read any tutorial on fitting IDW models? Can you edit your question to show the code you have used and where you have got stuck?
    – Spacedman
    Commented Jul 14, 2019 at 13:48
  • I re-uploaded my codes, it's really terrible.
    – CHOI
    Commented Jul 14, 2019 at 15:33

1 Answer 1

1

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.

3
  • Thanks for your advice. The reason why I've used a doubly-nested loop is the number of values in data. In the data, the number of values are 5. So, I tried to run the code by having doubly-nested loop code. Above the code you said, what if I have lots of values, how can I write down the code to interpolate the missing values?
    – CHOI
    Commented Jul 17, 2019 at 0:42
  • Thanks for your comments again. I have a question about choosing your advice on my problem. could you give me followings?
    – CHOI
    Commented Jul 17, 2019 at 8:25
  • I've given you the code to interpolate the missing values. Do you want to repeat this for five columns as responses?
    – Spacedman
    Commented Jul 17, 2019 at 8:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.