markovnovice markovnovice - 3 months ago 15
R Question

R: Discounting a matrix of costs

I have a matrix of costs I'd like to discount according to discount rate, x. Each column is a unique disease state and each row is a unique year. Currently I'm working off of this code:

((medexpCommercial)/((1 + dr)^as.integer(rownames(medexpCommercial))))


I was using the
as.integer(rownames(medexpCommercial)
as an attempt to get the row index number. Does anyone know of another method I can use to get the row index number to use in the discounting equation? Or of another method to discount the costs in my matrix?

Answer

Don't use

as.integer(rownames(medexpCommercial))

use

seq_len(nrow(medexp‌​Commercial))

The latter always works, while the first is buggy. What if you have row names like "A", "B", "C"? Even if you have row names like "1", "3", "2", but look, they are not sorted (this is particularly the case after some row reordering of your data frame).

Comments