Some sample data to make your problem reproducible:
n <- 100
ages <- ifelse(
runif(n) > 0.25,
sample(50, n, replace = TRUE),
0
)
## [1] 39 26 0 26 8 48 30 47 46 48 15 26 4 43 3 12 47 2 4 10 8 4 0 35 21
## [26] 34 2 4 9 15 0 0 0 27 0 35 11 24 20 35 27 0 0 16 33 18 34 2 1 31
## [51] 0 13 0 49 16 45 43 43 38 44 22 30 39 0 12 3 3 34 21 40 7 26 0 2 23
## [76] 0 46 50 24 33 32 0 8 26 40 12 0 28 35 33 30 20 14 47 10 4 31 0 4 42
First, replace the zeroes with NA
.
ages[ages == 0] <- NA
## [1] 39 26 NA 26 8 48 30 47 46 48 15 26 4 43 3 12 47 2 4 10 8 4 NA 35 21
## [26] 34 2 4 9 15 NA NA NA 27 NA 35 11 24 20 35 27 NA NA 16 33 18 34 2 1 31
## [51] NA 13 NA 49 16 45 43 43 38 44 22 30 39 NA 12 3 3 34 21 40 7 26 NA 2 23
## [76] NA 46 50 24 33 32 NA 8 26 40 12 NA 28 35 33 30 20 14 47 10 4 31 NA 4 42
Then you can use an interpolation function to replace the missing values. There are lots of such functions in R. Here I've used one from the pracma
package. This has several different interpolation algorithms for you to experiment with.
library(pracma)
interp1(seq_along(ages), ages)
## [1] 39 26 26 26 8 48 30 47 46 48 15 26 4 43 3 12 47 2 4 10 8 4 4 35 21
## [26] 34 2 4 9 15 15 15 15 27 27 35 11 24 20 35 27 27 27 16 33 18 34 2 1 31
## [51] 31 13 13 49 16 45 43 43 38 44 22 30 39 39 12 3 3 34 21 40 7 26 26 2 23
## [76] 23 46 50 24 33 32 32 8 26 40 12 12 28 35 33 30 20 14 47 10 4 31 31 4 42
interp1(seq_along(ages), ages, method = "linear")
## [1] 39.00000 26.00000 26.00000 26.00000 8.00000 48.00000 30.00000 47.00000
## [9] 46.00000 48.00000 15.00000 26.00000 4.00000 43.00000 3.00000 12.00000
## [17] 47.00000 2.00000 4.00000 10.00000 8.00000 4.00000 19.50000 35.00000
## [25] 21.00000 34.00000 2.00000 4.00000 9.00000 15.00000 18.00000 21.00000
## [33] 24.00000 27.00000 31.00000 35.00000 11.00000 24.00000 20.00000 35.00000
## [41] 27.00000 23.33333 19.66667 16.00000 33.00000 18.00000 34.00000 2.00000
## [49] 1.00000 31.00000 22.00000 13.00000 31.00000 49.00000 16.00000 45.00000
## [57] 43.00000 43.00000 38.00000 44.00000 22.00000 30.00000 39.00000 25.50000
## [65] 12.00000 3.00000 3.00000 34.00000 21.00000 40.00000 7.00000 26.00000
## [73] 14.00000 2.00000 23.00000 34.50000 46.00000 50.00000 24.00000 33.00000
## [81] 32.00000 20.00000 8.00000 26.00000 40.00000 12.00000 20.00000 28.00000
## [89] 35.00000 33.00000 30.00000 20.00000 14.00000 47.00000 10.00000 4.00000
## [97] 31.00000 17.50000 4.00000 42.00000