r - switch() based on two vectors -


i building function run inflation adjustment based on year. function have like

    cci <- function(cost, year){   switch(year,          '2009' = cost,          '2010' = cost/1.01298,          '2011' = cost/1.0455,          '2012' = cost/1.07334,          '2013' = cost/1.10387,          '2014' = cost/1.13042,          '2015' = cost/1.16645) } 

it works fine if feed 1 set of data, cci(100, '2010'). want run vector,

    cost year 1    100 2010 2    300 2015 3    222 2012 

it rises error saying switch() expr can length of 1.

i tried sapply() returns me table.

    cci <- function(cost, year){     sapply(year, switch,            '2009' = cost,            '2010' = cost/1.01298,            '2011' = cost/1.0455,            '2012' = cost/1.07334,            '2013' = cost/1.10387,            '2014' = cost/1.13042,            '2015' = cost/1.16645)   }  r> cci(c(100,200), c('2010', '2011'))        2010   2011 [1,]  98.72  95.65 [2,] 197.44 191.30 

i vecter calculation.

thanks , happy holloween!

mapply job, bunk comments.

mapply(cci, cost=c(100, 200), year=c('2010', '2011'))


Comments

Popular posts from this blog

matlab - error with cyclic autocorrelation function -

django - (fields.E300) Field defines a relation with model 'AbstractEmailUser' which is either not installed, or is abstract -

c# - What is a good .Net RefEdit control to use with ExcelDna? -