0

I have two NA's in my Data and I simply have to do a linear interpolation to find their value but I don't understand why it does not work. Here is the data. It is quite big.

Here is what i've tried:

id1 <- as.numeric(ID1)
anyNA(id1)
#There is 2 
sum(anyNA(id1))
is.na(id1)
na46 <- approx(x=c(95.4968:101.491), y=c(103.856 : 44.7562), method = "linear")
  • Welcome to SO :) adding a image of your data doesn't help too much, as we can't use it to run code and try to solve your problem. Read [How to make a great R reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/) and add your data to the question using the `dput()` function. – Ricardo Semião e Castro Nov 21 '22 at 22:17
  • To fill the `NA` values in `ID1` you need to pass it to `approx`, and your code is not doing it currently. Maybe you want something like `approx(x = as.numeric(Year), y = as.numeric(ID1))` – Ricardo Semião e Castro Nov 21 '22 at 22:21
  • Looking at the screen shot, you what to perform 2 interpolations, Once between `c(95.4968, 101.491)` and the other `c(103.856, 44.7562)` - this one rolls into the next region so you want to extrapolate from the previous 2. For the first one use `approx(x=c(95.4968, 101.491), xout=1.5, method = "linear")$y` – Dave2e Nov 21 '22 at 22:44

1 Answers1

0

Use na.approx. There are some arguments that can be used to specify how to deal with NA's on the ends. It assumes that the data is equally spaced unless you use the x= argument to specify some other x values. See ?na.approx for more details.

library(zoo)

y <- c(1, 2, NA, 4, 5, NA, 7)
na.approx(y)
## [1] 1 2 3 4 5 6 7

y2 <- c(NA, 1, NA, 2, NA)
na.approx(y2)
## [1] 1.0 1.5 2.0

na.approx(y2, na.rm = FALSE)
[1] NA 1.0 1.5 2.0 NA   

na.approx(y2, rule = 2)
## [1] 1.0 1.0 1.5 2.0 2.0

# BOD comes with R.  Create version where demand[5] is NA.
BOD2 <- transform(BOD, demand = replace(demand, 5, NA))
transform(BOD2, demand = na.approx(demand)) # without x=
##   Time demand
## 1    1    8.3
## 2    2   10.3
## 3    3   19.0
## 4    4   16.0
## 5    5   17.9
## 6    7   19.8
transform(BOD2, demand = na.approx(demand, x = Time)) # with x=
##   Time   demand
## 1    1  8.30000
## 2    2 10.30000
## 3    3 19.00000
## 4    4 16.00000
## 5    5 17.26667
## 6    7 19.80000
G. Grothendieck
  • 254,981
  • 17
  • 203
  • 341