I would like to increment numbers from the last observed number to the last number i.e. the NA values should be replaced with 35, 36, 38, 39
How can I go about it?
library(tidyverse)
trialdata <- tibble(
id = c(13, 8, 20, 34, 4, NA, NA, NA, NA, NA)
)
If your goal is to fill the NA id rows with a sequence that starts after the maximum non-NA value, then here's one way you could do it:
trialdata %>%
mutate(
id_filled = cumsum(is.na(id)) + max(id, na.rm = T),
id_filled = coalesce(id, id_filled)
)
id id_filled
<dbl> <dbl>
1 13 13
2 8 8
3 20 20
4 34 34
5 4 4
6 NA 35
7 NA 36
8 NA 37
9 NA 38
10 NA 39