Merge two data frame and replace the NA value in R

前端 未结 2 1205
小鲜肉
小鲜肉 2020-12-18 08:19

I have a main table(a), containing column: id, age, and sex. eg.

a <- data.frame(id=letters[1:4], age=c(18,NA,9,NA), sex=c(\"M\",\"F\",\"F\",\"M\"))
  id          


        
相关标签:
2条回答
  • 2020-12-18 08:40

    We can use data.table

    library(data.table)
    setDT(a)[b, agei := i.age, on='id'][is.na(age), age := agei][,agei:= NULL][]
    a
     #  id age sex
    #1:  a  18   M
    #2:  b  32   F
    #3:  c   9   F
    #4:  d  20   M
    
    0 讨论(0)
  • 2020-12-18 08:46

    Here is a dplyr solution:

    library(dplyr)
    
    c <- left_join(a,b, by = "id") %>% # this will generate age.x and age.y
      mutate(age = ifelse(is.na(age.x), age.y, age.x)) %>% # we generate a joint 'age' variable
      select(-age.y, -age.x) # drop the superfluous columns
    
    > c
      id sex age
    1  a   M  18
    2  b   F  32
    3  c   F   9
    4  d   M  20
    

    Note that this will throw you a warning that you try to join on factor levels. This is because the example data in the reproducible example was generated with stringsAsFactors = T.

    0 讨论(0)
提交回复
热议问题