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屋!