I need to merge two different size data frames. The larger one (df1
) has a column with several repeated values (licto
), the shorter one (df2
) has the column licto
, but its values are not repeated. df2 also has a ID column. I need a new column in df1
with the IDs in df2
, repeated according the repeated values in licto
. The example below probably make it clearer.
df1<-data.frame(licfrom=c(15470,16307,17121,15350,16982,17182,20319,16727,16946,16262,16605,
16607,15924,15399,15404,16739,16839,16842,16899,16157,15399),
licto=c(17121,17121,17121,16982,16982,16982,16982,16946,16946,16262,16607,
16607,15924,16839,16839,16839,16839,16839,16839,16157,15399))
.
df2<-data.frame(licto=c(17121,16982,16946,16607,15924,16839,16157,15399),
fisherID=c(160,760,770,406,106,2196,17323,2441))
My data frames look like this:
df1 df2
licfrom licto licto fisherID
15470 17121 17121 160
16307 17121 16982 760
17121 17121 16946 770
15350 16982 16262 947
16982 16982 16607 406
17182 16982 15924 106
20319 16982 16839 2196
16727 16946 16157 17323
16946 16946 15399 2441
16262 16262
16605 16607
16607 16607
15924 15924
15399 16839
15404 16839
16739 16839
16839 16839
16842 16839
16899 16839
16157 16157
15399 15399
And my final data frame should be like this:
licfrom licto fisherID
15470 17121 160
16307 17121 160
17121 17121 160
15350 16982 760
16982 16982 760
17182 16982 760
20319 16982 760
16727 16946 770
16946 16946 770
16262 16262 947
16605 16607 406
16607 16607 406
15924 15924 106
15399 16839 2196
15404 16839 2196
16739 16839 2196
16839 16839 2196
16842 16839 2196
16899 16839 2196
16157 16157 17323
15399 15399 2441
Any help will be appreciated, as I have spent several hour trying to merge as I need. I have used merge
and %in%
with no success. Thanks!
In R we use merge() function to merge two dataframes in R. This function is present inside join() function of dplyr package. The most important condition for joining two dataframes is that the column type should be the same on which the merging happens.
To combine data frames stored in a list in R, we can use full_join function of dplyr package inside Reduce function.
The merge() function in base R can be used to merge input dataframes by common columns or row names. The merge() function retains all the row names of the dataframes, behaving similarly to the inner join. The dataframes are combined in order of the appearance in the input function call.
You can do it simply with function merge()
.
merge(df1,df2,sort=FALSE)
licto licfrom fisherID
1 17121 15470 160
2 17121 17121 160
3 17121 16307 160
4 16982 15350 760
5 16982 16982 760
6 16982 20319 760
7 16982 17182 760
8 16946 16727 770
9 16946 16946 770
10 16607 16605 406
11 16607 16607 406
12 15924 15924 106
13 16839 15399 2196
14 16839 15404 2196
15 16839 16739 2196
16 16839 16839 2196
17 16839 16842 2196
18 16839 16899 2196
19 16157 16157 17323
20 15399 15399 2441
To be more explicit with the options, you might want to try this
merge(df1, df2, by = "licto", all = TRUE, sort = FALSE)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With