I have this data:
df <- tibble(a = c(NA, NA, 1, NA, NA, NA, 1, NA, NA))
I want to fill the NAs after the occurence of 1 with counts. Like this:
df2 <- tibble(a = c(NA, NA, 1, 2, 3, 4, 1, 2, 3))
I’ve no solution so far. Any ideas?
>Solution :
one possible way to solve this within the tidyverse
:
library(dplyr)
df %>%
# return TRUE or FALSE (1 or 0) if is not NA and run cummulative sum to identify groups
dplyr::mutate(grp = cumsum(!is.na(a))) %>%
# build grouping
dplyr::group_by(grp) %>%
# give rownumber per group if group != 0 (first rows until a = 1 for the frist time
dplyr::transmute(a = ifelse(grp != 0, dplyr::row_number(), NA)) %>%
# release groupings to prevent unwanted behaviour down stream
dplyr::ungroup() %>%
# unselect grp if you do not need it further on in your calculations
dplyr::select(-grp)
# A tibble: 9 x 1
a
<int>
1 NA
2 NA
3 1
4 2
5 3
6 4
7 1
8 2
9 3