Partially merge two datasets and fill in NAs in R

三世轮回 提交于 2019-12-01 15:29:08
user3200293

Answers as given in the comments to the question:

1: using base R

Method 1:

df_new$evmatch <- with(df_new, ifelse(is.na(evmatch), EVTYPE, evmatch))

Method 2:

df_new$evmatch[is.na(df_new$evmatch] <- df_new$EVTYPE[is.na(df_new$evmatch]

Note: Make sure that both vars are characters or erroneous results will occur. If needed transform with as.character.

2: using data.table

library(data.table)
setDT(df_new)[is.na(evmatch), evmatch := EVTYPE]

3: using dplyr

library(dplyr)
filter(df_new, is.na(evmatch) %>% 
         select(evmatch) <- filter(df_new, is.na(evmatch) %>% 
                                     select(EVTYPE)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!