我对 R 非常陌生,我正在尝试将数据字典定义映射到一组数据以制作更具可读性的文本。
例如,基于目前在 Kaggle 上的 Ames Iowa 住房数据集中的数据字典,我正在尝试绘制房屋的分区图。
mapping <- list(
'A'='Agriculture',
'C (all)'='Commercial',
'FV'='Floating Village Residential',
'I'='Industrial',
'RH'='Residential High Density',
'RL'='Residential Low Density',
'RP'='Residential Low Density Park',
'RM'='Residential Medium Density'
)
housingData$MSZoning <- as.factor(as.character(mapping[origData$MSZoning]))
然而,原始数据集并不包含所有这些数据点的值。
> table(origData$MSZoning)
C (all) FV RH RL RM
10 65 16 1151 218
用我的代码映射后,键值对不对齐。(例如,农业被映射到“C”。)我相信源数据中的空值会影响我的映射。
> table(housingData$MSZoning, origData$MSZoning)
C (all) FV RH RL RM
Agriculture 10 0 0 0 0
Commercial 0 65 0 0 0
Floating Village Residential 0 0 16 0 0
Industrial 0 0 0 1151 0
Residential High Density 0 0 0 0 218
确保这些键和值正确对齐的更合适的方法是什么?