Suppose I have a major dataset called "main" and three other datasets called "df1", "df2" and "df3".
- "main" includes: year, colA, colB, colC
- "df1" includes: year, colD
- "df2" includes: year, colE
- "df3" includes: year, colF
I want to merge colD, colE, colF of df1, df2, df3 to "main" using the year column to rename that merged column to "merged_value".
The logic is like this:
- If colA == "Category1", then merge df1 to "main"
- If colA == "Category2", then merge df2 to "main"
- If colA == "Category3", then merge df3 to "main"
The only way I could think of now is to break the main into 3 subsets and do the merge (shown below), but are there any simpler approach to this?
main_sub1 = main[colA=="Category1"][df1, on="year"]
main_sub2 = main[colA=="Category2"][df2, on="year"]
main_sub3 = main[colA=="Category3"][df3, on="year"]
main_comb = rbind(main_sub1, main_sub2, main_sub3)