Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Count rows after value 1 where rest of rows are NAs in r

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading