2-5 R语言基础 factor

2020-09-16 15:21:17 浏览数 (1)

#因子:分类数据 #有序和无序 #整数向量 标签label #Male/Female #常用于lm(),glm()

> x <- factor(c("female","female","female","male")) > y <- factor(c("female","female","female","male"),levels=c("male","female"))

> #table 对于当前的因子有一个整体的了解 > table(x) x female male 3 1

> #去掉因子的属性,看因子的内容 > unclass(x) [1] 1 1 1 2 attr(,"levels") [1] "female" "male"

> unclass(y) [1] 2 2 2 1 attr(,"levels") [1] "male" "female"

> class(x)#仍是因子类型 [1] "factor"

> class(y) [1] "factor"

> class(unclass(x))#去掉因子的属性,因子变为了整型 [1] "integer"

> class(unclass(y)) [1] "integer"

0 人点赞