本文章仅为菜鸟学生的学习笔记,一些碎碎念
factor 因子,个人感觉可以用来归类,统计各元素出现次数。
这是R documentation里给的解释:
Description:
The function
factor
is used to encode a vector as a factor (the terms ‘category’ and ‘enumerated type’ are also used for factors). If argumentordered
isTRUE
, the factor levels are assumed to be ordered. For compatibility with S there is also a functionordered
.函数因子用于将向量编码为因子(术语“类别”和“枚举类型”也用于因子)。如果参数ordered为TRUE,则假设因子级别是有序的。为了与S兼容,还有一个函数顺序。
Usage:
factor(x = character(), levels, labels = levels, exclude = NA, ordered = is.ordered(x), nmax = NA)
#举个例子
> data_example <- c(3,1,4,1,5,9,2,6,5,3,5,8)
> data_f <- factor(data_example)
> data_f
[1] 3 1 4 1 5 9 2 6 5 3 5 8
Levels: 1 2 3 4 5 6 8 9
> table(data_f)
data_f
1 2 3 4 5 6 8 9
2 1 2 1 3 1 1 1
levels:
#定义levels,按规定顺序排列
> data_f <- factor(data_example, levels=c(3,4,8,1))
> data_f
[1] 3 1 4 1 <NA> <NA> <NA> <NA> <NA> 3 <NA> 8
Levels: 3 4 8 1
> table(data_f)
data_f
3 4 8 1
2 1 1 2
labels:
#为各个level命名,若未划分levels,则按照默认顺序命名
> data_f <- factor(data_example, levels=c(3,4,8,1), labels=c("A","B","C","D"))
> data_f
[1] A D B D <NA> <NA> <NA> <NA> <NA> A <NA> C
Levels: A B C D
> table(data_f)
data_f
A B C D
2 1 1 2