r - Merge unequal dataframes and replace missing rows with 0 -
i have 2 data.frames, 1 characters , other 1 characters , values.
df1 = data.frame(x=c('a', 'b', 'c', 'd', 'e')) df2 = data.frame(x=c('a', 'b', 'c'),y = c(0,1,0)) merge(df1, df2) x y 1 0 2 b 1 3 c 0
i want merge df1 , df2. characters a, b , c merged , have 0, 1, 0 d , e has nothing. want d , e in merge table, 0 0 condition. every missing row @ df2 data.frame, 0 must placed in df1 table, like:
x y 1 0 2 b 1 3 c 0 4 d 0 5 e 0
take @ page merge. all
parameter lets specify different types of merges. here want set all = true
. make merge return na
values don't match, can update 0 is.na()
:
zz <- merge(df1, df2, = true) zz[is.na(zz)] <- 0 > zz x y 1 0 2 b 1 3 c 0 4 d 0 5 e 0
Comments
Post a Comment