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:
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.
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:
Make a test data set of 20 points with 20 N values that are the measurement we are interested in:
Let's set 3 of our measurements to
NA
because these are missing - we want to fill these locations in:Now run
idw
with our valid data aslocations
and our prediction locations asnewdata
. Note how we usevalid
as a true/false index to subset our data:Giving:
Note the prediction variance is
NA
because theidw
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:
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 ofnewdata
locations given a set of validlocation
data with coordinates and measurements.