I'd like some ideas/solutions for the following example. I have two tables, deals.df
and turnover.df
. Please see a toy example below which reflects what they look like:
deals.df <- data.frame(DealID = c(100101, 100102, 100103, 100104),
CompanyName= c('ABC', 'ABC', 'DEF', 'HIJ'),
DealYear = c(2013,2014,2015,2013),
DealYearL1= deals.df$DealYear-1,
DealYearP1= deals.df$DealYear+1,
DealYearL1Turnover= c('?', '?', '?', '?'),
DealYearTurnover= c('?', '?', '?', '?'),
DealYearP1Turnover= c('?', '?', '?', '?'))
turnover.df <- data.frame(CompanyName=c('ABC', 'DEF', 'HIJ'),
Turnover2011= c(100, 150, 180),
Turnover2012=c(110, 160, 200),
Turnover2013= c(125, 175, 210),
Turnover2014= c(135,180,230),
Turnover2015= c(145, 200, 235),
Turnover2016= c(160, 220, 250))
Note DealYearL1
means "Deal year less 1" and DealYearP1
means "Deal year plus one". What I want to do is populate the variables with "?". The logic is something like this:
For each DealID
in deals.df
, find the turnover associated with that CompanyName
in turnover.df
in the year before the deal, the year of the deal, and the year after the deal (ie the variables with '?' values). So for example the top row would look like this:
#Ideal output in deals.df
DealID CompanyName DealYear DealYearL1 DealYearP1 DealYearL1Turnover DealYearTurnover DealYearP1Turnover
100101 ABC 2013 2012 2014 110 125 135