I have 2 separate dataframes
df1
ID Name loq
1 a 1.2
1 b 1.4
1 c 1.0
2 a 0.5
2 b 0.7
2 c 0.3
3 a 0.5
3 b 0.2
3 c 0.1
df2 This is a list of dataframes
[1] ID Name
1 a
3 b
2 c
2 a
[2] ID Name
3 c
2 b
2 a
1 c
The result for df2 would look like this
[1] ID Name loq
1 a 1.2
3 b 0.2
2 c 0.3
2 a 0.5
[2] ID Name loq
3 c 0.1
2 b 0.7
2 a 0.5
1 c 1.0
And I have a long list of dataframes.
I would like add match ID AND name from df1 to df2 list and a new variable called loq to the df2 list based on the match.
I know how to do the match variable within a dataframe but don’t know how to match a df to a list of dataframes.
Keeping to base, it is pretty straight-forward to do what you want:
You could also manually specify what to merge on. In this case, it looks for matching column names: c(ID, Name). If you wanted to put df2 into a dataframe, you could do something like
The key here is that the columns ID and Name uniquely define the record (in database terms, they must be a composite primary key for that data table (frame)). In this way, merging (joining) the df2 list elements (data frames) by their matches in df1 will not run into duplication and therefore problems. This is to say, it will work as long as there’s not 2 different rows with the same ID and Name values.