r - How can I merge two dataframes if two cols have to be the same? -
this question has answer here:
i have 2 data frames. example df1
looks like:
name month number 1.h 1 8 2.h 2 7 3.h 3 6 4.a 1 9 5.a 2 10 6.a 3 11
and df2
looks like:
name month index 1.h 1 3 2.h 2 2 3.h 3 1 4.a 1 3 5.a 2 5 6.a 3 9
and want merge following df
:
name month number index 1.h 1 8 3 2.h 2 7 2 3.h 3 6 1 4.a 1 9 3 5.a 2 10 5 6.a 3 11 9
how can merge 2 df's df
?
i have tried merge
function by.x
, by.y
allows merging 1 column, want second column.
you can merge on more 1 column @ time:
merge(df1, df2, = c('name', 'month'))
in fact, should default, default value of by
intersect(names(df1), names(df2))
.
Comments
Post a Comment