markovnovice markovnovice - 2 months ago 5x
R Question

R: How to rescale my matrix by column

I have a matrix of disease states

, where the columns are states (i.e., "no disease", "disease", "dead") and the rows are model cycles (i.e., 1, 2, 3, 4, etc.).

I'm trying to multiply this by a vector of costs
, wherein each cost corresponds to a disease state. I've attempted the following:

commercialMedExp * states0CommercialA

but it appears as though the multiplication is occurring across columns instead of across rows. Could someone help me with the correct syntax?


Something like

commercialMedExp0A <- t(apply(states0CommercialA, 1, function(x){ x * commercialMedExp}))

should work so long as the number of columns in states0CommericialA is the same length as commercialMedExp. If it is not you would have to subset the data. For example, if the disease states are in columns 13 through 18

    commercialMedExp0A <- t(apply(states0CommercialA[,c(13:18)], 1, function(x){ x * commercialMedExp}))