• Welcome to LiuJason's Blog!

R语言中分类变量(factor)、水平(level)的修改与转换

R语言 Jason 5 years ago (2019-04-09) 6182 Views 0 Comments QR code of this page

变量可归结为类别(名义型),有序型,连续型变量(区间变量)。类别变量和有序类别(有序型)变量在R中称为因子(factor)。区间变量取连续的数值,可以进行求和、平均等运算。名义变量和有序变量取离散值,可以用数值代表也可以 是字符型值,其具体数值没有加减乘除的意义,不能用来计算而只能用来分类或者计数。名 义变量比如性别、省份、职业,有序变量比如班级名次。函数factor()以一个整数向量的形式存储类别值,整数的取值范围是[1……k](其中k是名义型变量中唯一值的个数),同时一个字符串(原始值)组成的内内部向量将映射到这些整数上

> haha<-c("aa","ab","ac",'aa')> haha

[1] "aa" "ab" "ac" "aa"

> ha<-factor(haha)> ha

[1] aa ab ac aa

Levels: aa ab ac

> h<-factor(haha,ordered=TRUE)> h

[1] aa ab ac aa

Levels: aa < ab < ac对于字符型向量,因子的水平默认依字母顺序创建,而按默认字母顺序排序的因子很少能够让人满意,所以可以通过levels选项来覆盖默认排序。例如status<-c("poor","improved","excellent","poor");status<-factor(status,ordered=TRUE) #会将编码为(3,2,1,3),并在内部将这关联为1=excellent,2=improved,3=poor。sattus<-factor(status,order=TRUE,levels=c("poor","improved","excellent")) #这样各水平的排序为1=poor,2=improved,3=excellent。 levels 用来指定因子可能的水平(缺省值是向量x中互异的值),表示这组离散值;labels用来指定水平的名字;exclude表示从向量x中剔除的水平值;ordered是一个逻辑型选项用来指定因子的水平是否有次序。回想数值型或字符型的x。下面有一些例子:> factor(1:3)

[1] 1 2 3

Levels: 1 2 3

> factor(1:3, levels=1:5)

[1] 1 2 3

Levels: 1 2 3 4 5

> factor(1:3, labels=c("A", "B", "C"))

[1] A B C

Levels: A B C

> factor(1:5, exclude=4)

[1] 1 2 3 NA 5

Levels: 1 2 3 5

函数levels用来提取一个因子中可能的水平值:

> ff <- factor(c(2, 4), levels=2:5)> ff

[1] 2 4

Levels: 2 3 4 5

> levels(ff)

[1] "2" "3" "4" "5"

因子用来存储类别变量(categorical variables)和有序变量,这类变量不能用来计算而只能用来分类或者计数。因子表示分类变量,有序因子表示有序变量。一个因子不仅包括分类变量本身还包括变量不同的可能水平(即使它们在数据中不出现)。因子函数factor用下面的选项创建一个因子:

factor(x, levels = sort(unique(x), na.last = TRUE),

labels = levels, exclude = NA, ordered = is.ordered(x))

其中x是数据,levels是因子水平向量,labels是因子的

标签向量。

1、 创建一个因子。

> colour <- c('G', 'G', 'R', 'Y', 'G', 'Y', 'Y', 'R', 'Y')> colour

[1] "G" "G" "R" "Y" "G" "Y" "Y" "R" "Y"

> col <- factor(colour)> col

[1] G G R Y G Y Y R Y

Levels: G R Y

> col1 <- factor(colour, levels = c('G', 'R', 'Y'), labels = c('Green', 'Red', 'Yellow'))> col1

[1] Green Green Red Yellow Green Yellow

[7] Yellow Red Yellow

Levels: Green Red Yellow

> col2 <- factor(colour, levels = c('G', 'R', 'Y'), labels = c('1', '2', '3'))> col2

[1] 1 1 2 3 1 3 3 2 3

Levels: 1 2 3

> col_vec <- as.vector(col2) #转换成字符向量> col_vec

[1] "1" "1" "2" "3" "1" "3" "3" "2" "3"

> col_num <- as.numeric(col2) #转换成数字向量> col_num

[1] 1 1 2 3 1 3 3 2 3

> col3 <- factor(colour, levels = c('G', 'R'))> col3

[1] G G R G R

[9]

Levels: G R

2、创建一个有序因子。

score <- c('A', 'B', 'A', 'C', 'B')score1 <- ordered(score, levels = c('C', 'B', 'A'));> score1

[1] A B A C B

Levels: C < B < A 3、用cut()函数将一般的数据转换成因子或有序因子。exam <- c(98, 97, 52, 88, 85, 75, 97, 92, 77, 74, 70, 63, 97, 71, 98, 65, 79, 74, 58, 59, 60, 63, 87, 82, 95, 75, 79, 96, 50, 88)exam1 <- cut(exam, breaks = 3) #切分成3组exam2 <- cut(exam, breaks = c(0, 59, 69, 79, 89, 100)) #切分成自己设置的组attr(exam1, 'levels'); attr(exam2, 'levels'); attr(exam2, 'class')ordered(exam2, labels = c('bad', 'ok', 'average', 'good', 'excellent')) #一个有序因子 4.table计算每个因子出现的次数> table(score1)

score1

C B A

1 2 2

如何去除factor中冗余的level

> ff <- factor(1:100)> ff

[1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25

[26] 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50

[51] 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75

[76] 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100

100 Levels: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 ... 100

> ff2 <- ff[1]> ff2

[1] 1

100 Levels: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 ... 100

解决办法:factor(ff2)

其实啊,这个levels就是表示所有可能的取值(也就是对因子向量进行分组,相同的归为一组,每组为一个Level,有点Unique的味道),并有一个顺序

reorder

reorder.default {stats} R Documentation

Reorder Levels of a Factor

reorder is a generic function. The "default" method treats its first argument as a categorical variable, and reorders its levels based on the values of a second variable, usually numeric.

根据第二列的值,来重新拍一下第一列的Levels。

Usage

reorder(x, ...)

## Default S3 method:

reorder(x, X, FUN = mean, ...,

order = is.ordered(x))

require(graphics)

bymedian <- with(InsectSprays, reorder(spray, count, median))boxplot(count ~ bymedian, data = InsectSprays, xlab = "Type of spray", ylab = "Insect count", main = "InsectSprays data", varwidth = TRUE, col = "lightgray")


This article is under CC BY-NC-SA 4.0 license.
Please quote the original link:https://www.liujason.com/article/261.html
Like (6)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址