Search code examples
rfill

Filling the missing values within each id in r


I have a dataframe having some rows missing value. Here is a sample dataframe:

df <- data.frame(id = c(1,1,1, 2,2,2, 3,3,3),
                 item = c(11,12,13, 24,25,26, 56,45,56),
                 score = c(5,5, NA, 6,6,6, 7,NA, 7))

> df
  id item score
1  1   11     5
2  1   12     5
3  1   13    NA
4  2   24     6
5  2   25     6
6  2   26     6
7  3   56     7
8  3   45    NA
9  3   56     7

Grouping the dataset by id column, I would like to fill those NA values with the same score.

the desired output should be:

> df
  id item score
1  1   11     5
2  1   12     5
3  1   13     5
4  2   24     6
5  2   25     6
6  2   26     6
7  3   56     7
8  3   45     7
9  3   56     7

Any ideas?

Thanks!


Solution

  • We can group by 'id' and fill

    library(dplyr)
    library(tidyr)
    df %>%
       group_by(id) %>% 
       fill(score, .direction = "downup") %>%
       ungroup