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

Time:11-24

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 

CodePudding user response:

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

CodePudding user response:

df %>%
   mutate(id = ifelse(type == 'P', id, NA))%>%
   fill(id)

   type  id
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
  • Related