In R I need to create a new dataframe (DF3) and map some data from two existing dataframes (DF1 and DF2). Some mapped fields will be net-new, some mapped fields will be existing with same name and some fields will be existing under a different name. The basic framework is this:
D1 = data.frame(
"FieldA" = c("apple","banana","grapes","pear","orange"),
"FieldB" = c(1,2,3,4,5),
"FieldC" = c(5,4,2,3,1),
"FieldD" = c(9,8,7,6,5))
D2 = data.frame(
"FieldA" = c("bread","cereal","milk","oatmeal","smoothie"),
"FieldB" = c(1,2,3,4,5),
"FieldC" = c(5,4,2,3,1),
"FieldX" = c(9,8,7,6,5),
"FieldY" = c(3,4,5,6,7))
D3 = D1[,c("FieldA","FieldB")]
Using the above I am able to map DF1 fields to DF3. But I can't figure out how to bring over DF2 rows whilst mapping the three DF2 fields I need:
- DF2 "FieldA" mapped to existing DF3 "FieldA"
- DF2 "FieldX" mapped as a new column in DF3
- DF2 "FieldY" to existing column DF3 "FieldB"
Results of DF3 should be 10 total rows of data with column fields "FieldA", "FieldB", "FieldX"