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


Read more here: https://stackoverflow.com/questions/66344131/r-dplyr-perform-interpolation-for-each-group-with-varying-min-and-max

Content Attribution

This content was originally published by Afiq Johari at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: