Linear interpolation of missing dependent values based on an independent variable, not permitting extrapolation
R
Code snippet
Published
July 10, 2024
Linear interpolation of missing dependent values based on an independent variable Only interpolation permitted – values requiring extrapolation would remain NA.
One use case is data collected from multiple patients (IDs), with a column of ages, and a column of weights where some weights are missing (NA). For each ID, weights will be linearly interpolated based on the ages.
df %>%# Group by id and interpolate missing y values based on surrounding x and y valuesgroup_by(id) %>%mutate(y_interpolate =round(interpolate_missing(x, y), 2)) %>%arrange(id, x) %>% knitr::kable()