简体   繁体   中英

R dplyr perform interpolation for each group with varying min and max

Each segment has different range , for example A is from 1 to 3 while C is from 1 to 7. For each segment, there can be missing time for which I want to perform interpolation (linear, spline, etc)

How can I do it within dplyr?

have <- data.frame(time   =c(1,3,1,2,5,1,3,5,7),
                 segment=c('A','A','B','B','B','C','C','C','C'),
                 toInterpolate= c(0.12,0.31,0.15,0.24,0.55,0.11,0.35,0.53,0.79))
have


want <- data.frame(time   =c(1,2,3,1,2,3,4,5,1,2,3,4,5,6,7),
                   segment=c('A','A','A','B','B','B','B','B','C','C','C','C','C','C','C'),
                   Interpolated= c(0.12,0.21,0.31,0.15,0.24,0.34,0.41,0.55,0.11,0.28,0.35,0.45,0.53,0.69,0.79))
# note that the interpolated values here are just randomnly put, (not based on actual linear/spline interpolation)

want

We can use complete to complete the sequence and na.spline from zoo for interpolation.

library(dplyr)
library(tidyr)
library(zoo)

have %>%
  group_by(segment) %>%
  complete(time = min(time):max(time)) %>%
  mutate(toInterpolate = na.spline(toInterpolate))

#  segment  time toInterpolate
#   <chr>   <dbl>         <dbl>
# 1 A           1         0.12 
# 2 A           2         0.215
# 3 A           3         0.31 
# 4 B           1         0.15 
# 5 B           2         0.24 
# 6 B           3         0.337
# 7 B           4         0.44 
# 8 B           5         0.55 
# 9 C           1         0.11 
#10 C           2         0.246
#11 C           3         0.35 
#12 C           4         0.439
#13 C           5         0.53 
#14 C           6         0.641
#15 C           7         0.79 

To create sequence for smaller granularities.

have %>%
  group_by(segment) %>%
  complete(time = min(time):max(time)) %>%
  mutate(toInterpolate = na.spline(toInterpolate)) %>%
  complete(time = seq(min(time), max(time), 0.1))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM