I am currently working with four multiple data frames listed below.
- tmp: Master table (variables included: group, weight, country, revenue)
- price1: Price table 1 for group 1 - 3(variables included: group, weight, price)
- price2: Price table 2 for group 4 - 8(variables included: group, price)
- price3: Price table 3 for group 9(variables included: group, country, weight, price)
"weight" is an integer variable starting from 1 to 200. In price table 1 and 3, there is a price value assigned to each "weight".
I want to keep the master table as is and conditionally merge the price table 1,2,3 to it. The "group" variable in master table range from 1 to 9. The price tables were managed based on the "group" value as well. Each value represents one single product. Product in the same price table has similar nature with different price. However, products across price tables have different nature. Because of this difference, the merge criteria is different as well.
The purpose of this merge is to compare revenue to price for different products.
The following is the merge criteria between master table and 3 price table.
- Primary key in Price table 1 to master table: group & weight
- Primary key in Price table 2 to master table: group
- Primary key in Price table 3 to master table: group & country & weight
Used code:
test <- ifelse(tmp$group %in% c(1,2,3), merge(tmp,price1,by=c("group","weight"))
,ifelse(tmp$group %in% c(4,5,6,7,8),merge(tmp, price2, by= "group")
,merge(tmp,price3, by=c("group","country","weight")))))
The problem with my code is that the return object is not a data frame, it showed as a large list in R.