【发布时间】:2014-03-30 04:29:16
【问题描述】:
假设我读过一个数据框,其中一列包含字符串作为因子。我想将因子转换为数字,但要使用特定映射。这种转换通常是稍后计算的前导步骤。例如:
> library(rpart)
> head(car90["Type"])
Type
Acura Integra Small
Acura Legend Medium
Audi 100 Medium
Audi 80 Compact
BMW 325i Compact
BMW 535i Medium
> summary(car90$Type)
Compact Large Medium Small Sporty Van NA's
19 7 26 22 21 10 6
在 car90$Type 列中,我想将“Compact”设置为 -10,“Large”设置为 -1,“Medium”设置为 0,“Small”设置为 1,“Sporty”设置为10 和 'Van' 为 20,其中数字是数字,而不是因子。我该怎么做?
我已经查看了相关问题,但没有一个提供解决方案。
Replace specific column "words" into number or blank
Changing column names of a data frame in R
【问题讨论】: