How do I interpolate for the NA values in a data frame?

Goal

I have a list of data frames whose dimensions are smaller than data frames in a second list. I would like to left merge them and then interpolate, to fill in the missing values. I have tried using smooth.spline() but this doesn't work with the NA's. Any suggestions?

Reprex

x <- tibble::tibble(
  x = 1:9, 
  y = 1
)


y <- tibble::tibble(
  x = c(1,3,5,6,9),
  alpha = c(2,7,4,2,8)
)


z = dplyr::left_join(x = x, y = y)

z %>% dplyr::mutate(
     beta = stats::smooth.spline(x = alpha, spar = 0.5)$y
     )

I think its very difficult to advise you without context.

the alpha seems rather random, and doesnt seem like there would be reasonable imputations to be made for a cubic spline, I would expect in most cases something like midpoint linear interpolation to be a reasonable guess.


z %>% mutate(b1=alpha,
             b2=alpha) %>% 
  tidyr::fill(b1,.direction="down") %>%
  tidyr::fill(b2,.direction="up") %>%
  mutate(beta=ifelse(is.na(alpha),(b1+b2)/2,alpha)) -> z2

This looks great, except I am trying to avoid repeated values. My data represents sea level elevation at times throughout history but I only have data for certain time points. I am trying to load this data into a model but the behavior of sea level elevation changes is likely smooth rather than a step-function pattern.

So is it a time series then, a what resolution do you have minutes, hours, days?

It is a time serious, this is a geological study so its measured in 1 million year time segments.

How many millions of years do you have ? If its only a few thousand you could share it all here presumably

I have a 2 list with 23 data frames for the data I am working with, sort of. I am working to wrangle one list from characters vectors into dbl formatted tibble's, and then I will have the two lists. from there I was going to:

pmap(list1, list2, function(a,b){
left_join(a,b) %>%
     mutate(

     )
})

Until I get my character vectors into the tibble's I don't have the actual data to share.

Could I apply a cubic.spline to the method you suggested as long as I keep the spar variable low?

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.