Change fill() value

Hey there,

I used map_df for generating a full dataset and now want to fill the gaps (here you can read how that was done if you are interested).

Now, my problem is, that I cannot use fill() for all variables, because some like age change automatically. I found a workaround for this, but i seem to be over-complicated.

Any ideas how to keep it simple?

complete.df <- complete.df %>%
  fill(sex) %>% 
  group_by(id,year) %>% 
  fill(age, .direction = "downup") %>% 
  ungroup(year) %>% 
  mutate(age = ifelse(flag_fill==1 & month==1, lag(age)+1, ifelse(flag_fill==1 & month==12, lead(age)-1, age))) %>% 
  group_by(id,year) %>% 
  fill(age, .direction = "downup") %>% 
  ungroup(year) %>%  
  mutate(age = ifelse(flag_fill==1 & month==1, lag(age)+1, ifelse(flag_fill==1 & month==12, lead(age)-1, age))) %>% 
  group_by(id,year) %>% 
  fill(age, .direction = "downup")

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.