Home > Software engineering >  In R: Replace NAs with values of other row but same value in other column
In R: Replace NAs with values of other row but same value in other column

Time:11-24

I have a much larger dataset. This is just a short example. What i intend to do is to replace the NAs. Particularly, i want to replace them with the value that already exists for the same id. I would prefer a pipe solution from dplyr.

ID <- c(1, 1, 1, 2, 2, 3, 3)
var <- c(NA, NA, 'M', 'F', NA, NA, 'M')
df <- data.frame(ID,var)
df %>% group_by(ID) #i am pretty sure that i need that
#df %>% replace_na(list(var)) #this is what i tried but does not work

The solution would be:

var <- c('M', 'M', 'M', 'F', 'F', 'M', 'M')

CodePudding user response:

You can try with fill():

library(dplyr)
library(tidyr) 
    
df %>%
  group_by(ID)  %>% 
  fill(var, .direction = "updown")


# A tibble: 7 x 2
# Groups:   ID [3]
     ID var  
  <dbl> <chr>
1     1 M    
2     1 M    
3     1 M    
4     2 F    
5     2 F    
6     3 M    
7     3 M   

CodePudding user response:

See Replace NA with values in another row of same column for each group in r Here is the solution of your question:

library(data.table)
setDT(df)[, var:= var[!is.na(var)][1L] , by = ID]
df
  • Related