我有几个变量,
qual_cols <- c("ExterQual", "ExterCond", "BsmtQual", "BsmtCond", "HeatingQC", "KitchenQual", "FireplaceQu", "GarageQual", "GarageCond", "PoolQC")
每列由相同的五个 chr 值组成:
grades <- c( "Po", "Fa", "TA", "Gd", "Ex")
我想数字化
"Po" = 1; "Fa" = 2; "TA" = 4 "Gd" = 6 "Ex" = 11
为了乘以创建新变量,如下所示:
combi$GarageGrade <- combi$GarageQual * combi$GarageCond
combi$ExterGrade <- combi$ExterQual * combi$ExterCond
combi$KitchenScore <- combi$KitchenAbvGr * combi$KitchenQual
有哪些方法可以实现这一目标?
注意:我是编程的初学者,所以夸大其词是值得赞赏的。
例子:
head(combi$ExterQual) # (Gd, TA, Gd, TA, Gd, Ta)
价值等价物将是 (6, 4, 6, 4, 6, 4)
head(combi$ExterCond) # (TA, TA, TA, TA, TA, TA)
价值等价物将是 (4, 4, 4, 4, 4, 4)
combi$ExterGrade <- combi$ExterQual * combi$ExterCond
head(combi$ExterGrade) # expected output: (24, 16, 24, 16, 24, 16)