How can I merge the content two data.frames / tables without adding additional columns?
Thanks!
How can I merge the content two data.frames / tables without adding additional columns?
Thanks!
 
    
    This will work for you if you want to merge by column "ID" in your two datasets.
merge(x = TABLE1, y = TABLE2[ , c("Name", "Date", "ID")], by = "ID", all=TRUE)
I am sub-setting the second dataset while merging. If you want to merge by Name, Date and ID columns then you can do as below:
merge(x = TABLE1, y = TABLE2[ , c("Name", "Date", "ID")], by = c("Name", "Date", "ID"), all=TRUE)
#Output
  Name       Date      ID   Price
1 Green       <NA>  KF3902    NA
2  Pink       <NA>  F43346    NA
3  <NA> 2017-08-04     AMA     2
4  <NA> 2017-09-29    <NA>    33
 
    
    Here is a solution with dplyr:
library(dplyr)
full_join(table1, table2, 
          by = c("Name", "Date", "ID")) %>%
  select(-Cost, -PNL) %>%
  rename(Item = ID)
Result:
   Name       Date Price   Item
1  <NA> 2017-09-29    33   <NA>
2  <NA> 2017-08-04     2    AMA
3  Pink       <NA>    NA F43346
4 Green       <NA>    NA KF3902
Notes:
My solution made some assumptions about the datasets table1 andtable2. For example, whether Date has already been converted to a date variable, and whether the Name and ID variables is character type instead of factor. I will therefore adjust my solution once the OP provides data.
Data:
table1 = data.frame(Name = as.character(c(NA, NA)),
                    Date = as.Date(c("2017-9-29", "2017-8-4")),
                    Price = c(33, 2),
                    ID = c(NA, "AMA"),
                    stringsAsFactors = FALSE)
table2 = data.frame(Name = c("Pink", "Green"),
                    Cost = c(25, 876),
                    PNL = c(11, 252),
                    Date = as.Date(c(NA, NA)),
                    ID = c("F43346", "KF3902"),
                    stringsAsFactors = FALSE)
