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

repeat values in a dataframe column and change it when another column changes

I have the following dataframe :

# A tibble: 15 × 2
   type  id   
   <chr> <chr>
 1 P     A1   
 2 N     A2   
 3 N     A3   
 4 N     A4   
 5 P     A5   
 6 N     A6   
 7 N     A7   
 8 P     A8   
 9 N     A9   
10 N     A10  
11 P     A11  
12 N     A12  
13 N     A13  
14 N     A14  
15 P     A15

The correct id for each type is the id that is where the is type = "P" and stays the same until another type "P" appears again and the following id’s take it’s id. basically i want the following:

# A tibble: 15 × 2
   type  id   
   <chr> <chr>
 1 P     A1   
 2 N     A1   
 3 N     A1   
 4 N     A1   
 5 P     A5   
 6 N     A5   
 7 N     A5   
 8 P     A8   
 9 N     A8   
10 N     A8   
11 P     A11  
12 N     A11  
13 N     A11  
14 N     A11  
15 P     A15 

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 :

This does it:

df %>% mutate(group = cumsum(type == 'P')) %>%
  group_by(group) %>% mutate(id = first(id))  %>% ungroup %>% select(-group)

# type  id   
# <fct> <fct>
# 1 P     A1   
# 2 N     A1   
# 3 N     A1   
# 4 N     A1   
# 5 P     A5   
# 6 N     A5   
# 7 N     A5   
# 8 P     A8   
# 9 N     A8   
# 10 N     A8   
# 11 P     A11  
# 12 N     A11  
# 13 N     A11  
# 14 N     A11  
# 15 P     A15
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