I have 2 dataframes in R, I want to use all the columns from the first dataframe and add a column from a second dataframe. However, which column from the second dataframe will be determine by a value in the first dataframe. I have tried merge, with, and others but just not sure what I am doing.
DataFrame 1
ODS_Data= data.frame(ContractNumber=c(1,2,1,2),
TransactionEffectiveDateAdj=as.Date(c('2016-08-01','2016-08-01','2016-09-01','2016-09-01')),
TransactionTypeCT=c('AF','FS','SW','FS'), GrossAmount=c(100,555,478,632))
DataFrame 2
OLAP_Data= data.frame(PolicyNumber=c(1,2,1,2),
AsOfDate=as.Date(c('2016-08-01','2016-08-01','2016-09-01','2016-09-01')),
AFGrossAmount=c(100,0,642,0), FSGrossAmount=c(0,555,0,632), SWGrossAmount=c(0,0,345,0))
The end result dataframe:
Comparison <- data.frame(TrxEffectiveDateAdj=as.Date(character()),
PolicyNumber=character(),
TransactionType=character(),
ODSTotalGrossAmount=num(),
OLAPTotalGrossAmount=num(),
CompareResult=character())
How the Comparison dataframe should be populated:
Comparison$TrxEffectiveDateAdj = ODS_Data$TransactionDateAdj
Comparison$PolicyNumber = ODS_Data$ContractNumber
Comparison$TransactionType = ODS_Data$TransactionTypeCT
Comparison$ODSTotalGrossAmount = ODS_Data$GrossAmount
Comparison$OLAPTotalGrossAmount = If ODS_Data$TransactionTypeCT = 'AF' then OLAP_Data$AFGrossAmount
ElseIf ODS_Data$TransactionTypeCT = 'FS' Then OLAP_Data$FSGrossAmount
ElseIf ODS_Data$TransactionTypeCT = 'SW' then OLAP_Data$SWGrossAmount
End IF
Comarison$CompareResult = Comparison$ODSTotalGrossAmount - Comparison$OLAPTotalGrossAmount
Also, the data between the dataframes should be matched on ODS_Data$ContractNumber = OLAP_Data$PolicyNumber AND ODS_Data$TransactionEffectiveDateAdj = OLAP_Data$AsOfDate
That is a lot of information, but I am new to R and I would really appreciate the help. Thanks!