如何通过匹配r中另一个数据帧的ID来仅替换NAs

如何通过匹配r中另一个数据帧的ID来仅替换NAs,r,dplyr,R,Dplyr,我的df1看起来像 PID End_record_date 123 NA 123 15-08-2020 234 NA 234 19-07-2020 345 NA 我的df2有匹配的ID PID Record_date 123 13-10-2018 234 14-07-2019 345 20-08-2020 我需要的预期结果 PID End_record_date 123 13-10-2018 123 15-08-2020 234 14-07-2019 234 19-07-2020 345 2

我的df1看起来像

PID End_record_date
123 NA
123 15-08-2020
234 NA
234 19-07-2020
345 NA
我的df2有匹配的ID

PID Record_date
123 13-10-2018
234 14-07-2019
345 20-08-2020
我需要的预期结果

PID End_record_date
123 13-10-2018
123 15-08-2020
234 14-07-2019
234 19-07-2020
345 20-08-2020
我只需要在不影响其他值的情况下填充NAs


谢谢

您可以使用
匹配

inds <- is.na(df1$End_record_date)
df1$End_record_date[inds] <- df2$Record_date[match(df1$PID[inds], df2$PID)]
df1

#  PID End_record_date
#1 123      13-10-2018
#2 123      15-08-2020
#3 234      14-07-2019
#4 234      19-07-2020
#5 345      20-08-2020
或在
dplyr
中:

library(dplyr)

inner_join(df1, df2, by = 'PID') %>%
  mutate(End_record_date = coalesce(End_record_date,Record_date)) %>%
  select(PID, End_record_date)

您可以使用
匹配

inds <- is.na(df1$End_record_date)
df1$End_record_date[inds] <- df2$Record_date[match(df1$PID[inds], df2$PID)]
df1

#  PID End_record_date
#1 123      13-10-2018
#2 123      15-08-2020
#3 234      14-07-2019
#4 234      19-07-2020
#5 345      20-08-2020
或在
dplyr
中:

library(dplyr)

inner_join(df1, df2, by = 'PID') %>%
  mutate(End_record_date = coalesce(End_record_date,Record_date)) %>%
  select(PID, End_record_date)