i have 2 datasets
a
= raw dataset thousands of observations of different weather events
state evtype 1 al winter storm 2 al tornado 3 al tstm wind 4 al tstm wind 5 al tstm wind 6 al hail 7 al high wind 8 al tstm wind 9 al tstm wind 10 al tstm wind
b
= dictionary table, has standard spelling weather events.
evtype evmatch 1 high surf advisory <na> 2 coastal flood coastal flood 3 flash flood flash flood 4 lightning lightning 5 tstm wind <na> 6 tstm wind (g45) <na>
both merged df_new
evtype
library(dplyr) df_new <- left_join(a, b, = c("evtype")) state evtype evmatch 1 al winter storm winter storm 2 al tornado na 3 al tstm wind thunderstorm wind 4 al tstm wind thunderstorm wind 5 al tstm wind thunderstorm wind 6 al hail na 7 al high wind high wind 8 al tstm wind thunderstorm wind 9 al tstm wind thunderstorm wind 10 al tstm wind thunderstorm wind 11 al heavy rain na 12 al flash flood na 13 al tstm wind thunderstorm wind 14 al heavy rain na 15 al tstm wind thunderstorm wind
fill in missing nas
as can see in df_new$evmatch
, there nas. how can merge dataset, have na's in evmatch
filled in corresponding word evtype
. example...
wanted output
state evtype evmatch 1 al winter storm winter storm 2 al tornado tornado 3 al tstm wind thunderstorm wind 4 al tstm wind thunderstorm wind 5 al tstm wind thunderstorm wind 6 al hail hail 7 al high wind high wind 8 al tstm wind thunderstorm wind 9 al tstm wind thunderstorm wind 10 al tstm wind thunderstorm wind 11 al heavy rain heavy rain 12 al flash flood flash flood 13 al tstm wind thunderstorm wind 14 al heavy rain heavy rain 15 al tstm wind thunderstorm wind
answers given in comments 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 both vars characters or erroneous results occur. if needed transform 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)
Comments
Post a Comment