Search code examples
rdataframeconditional-statementsnafill

R data frame - fill missing values with condition on another column


In R, I have a the following data frame:

Id Year Age
1 2000 25
1 2001 NA
1 2002 NA
2 2000 NA
2 2001 30
2 2002 NA

Each Id has at least one row with age filled. I would like to fill the missing "Age" values with the correct age for each ID.

Expected result:

Id Year Age
1 2000 25
1 2001 25
1 2002 25
2 2000 30
2 2001 30
2 2002 30

I've tried using 'fill':

df %>% fill(age)

But not getting the expected results. Is there a simple way to do this?


Solution

  • The comments were close, you just have to add the .direction

    df %>% group_by(Id) %>% fill(Age, .direction="downup")
    # A tibble: 6 x 3
    # Groups:   Id [2]
         Id  Year   Age
      <int> <int> <int>
    1     1  2000    25
    2     1  2001    25
    3     1  2002    25
    4     2  2000    30
    5     2  2001    30
    6     2  2002    30