Linear Interpolation using dplyr

后端 未结 2 1610
谎友^
谎友^ 2020-12-17 20:54

I\'m trying to use the na.approx() function from the zoo library (in conjunction with xts) to interpolate missing values from repeated

2条回答
  •  离开以前
    2020-12-17 21:40

    The solution I've gone with is based on the first comment from @docendodiscimus

    Rather than attempt to create a new data frame as I'd been doing this approach simply adds columns to the existing data frame by taking advantage of dplyr's mutate() function.

    My code is now...

    df %>%
      group_by(variable) %>%
        arrange(variable, event.date) %>%
          mutate(ip.value = na.approx(value, maxgap = 4, rule = 2))
    

    The maxgap allows upto four consecutive NA's, whilst the rule option allows extrapolation into the flanking time points.

提交回复
热议问题