对R中的因子进行标记的理由是什么?

时间:2017-03-17 05:33:48

标签: r

无论如何,因子在内部存储为编码整数。然后标记它的用户是什么?

例如,假设我创建了一个简单的因素: -

> x<-c("Manager","Engineer","Manager","Manager","Manager","Engineer","Director")
> designation<-factor(x)
> designation
[1] Manager  Engineer Manager  Manager  Manager  Engineer Director
Levels: Director Engineer Manager
> str(designation)
Factor w/ 3 levels "Director","Engineer",..: 3 2 3 3 3 2 1

现在,因为&#39;指定&#39;是一个因素,在内部它被存储为整数。就我们的便利性而言,R已经将它作为字符串显示给我们,因为它知道这些字符串和整数之间的映射,因为它必须在某处存储元数据。那么在哪里需要标记呢?为什么R必须支持这种标记功能?

1 个答案:

答案 0 :(得分:0)

labels中的

factor()参数将允许您更改级别的名称。

例如,我要求Manager应为MDDirector应为ExecEngineer应为Asst_Commiss,然后我可以使用现有因子并使用labels参数更改其名称。

x <- c("Manager","Engineer","Manager","Manager","Manager","Engineer","Director")
designation <- factor(x)
designation

new_desig <- factor( designation,
                     levels = c( 'Manager', 'Director', 'Engineer'),
                     labels = c( 'MD', 'Exec', 'Asst_Commiss') )

new_desig
# [1] MD           Asst_Commiss MD           MD           MD           Asst_Commiss
# [7] Exec        
# Levels: MD Exec Asst_Commiss