R:如何按列重新缩放矩阵 [英] R: How to rescale my matrix by column
问题描述
我有一个疾病状态states0CommercialA
的矩阵,其中列是状态(即无疾病",疾病",死亡"),行是模型周期(即1、2、3, 4等).
I have a matrix of disease states states0CommercialA
, where the columns are states (i.e., "no disease", "disease", "dead") and the rows are model cycles (i.e., 1, 2, 3, 4, etc.).
我正试图将其乘以成本commercialMedExp
的向量,其中每个成本对应于一种疾病状态.我尝试了以下操作:
I'm trying to multiply this by a vector of costs commercialMedExp
, 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?
推荐答案
类似
commercialMedExp0A <- t(apply(states0CommercialA, 1, function(x){ x * commercialMedExp}))
只要states0CommericialA
中的列数与commercialMedExp
的长度相同,
就应该起作用.如果不是,则必须对数据进行子集化.例如,如果疾病状态在第13至18列
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}))
这篇关于R:如何按列重新缩放矩阵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!