Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Filling the missing values within each id in r

Tags:

r

fill

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!

like image 577
amisos55 Avatar asked Oct 11 '25 18:10

amisos55


1 Answers

We can group by 'id' and fill

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

Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!