factor 因子
在R中数据的分类是用因子数据类型(factor)来表示的。如性别。
> #数据框 data.frame
> ID <- c(1:4)
> age <- c(25,34,28,72)
> treatment <- c("type1","type2","type3","type1")
> status <- c("poor","stable","poor","stable")
> pdata <- data.frame(ID,age,treatment,status)
> colnames <- c("ID","age","treatment","status")
> rownames <- pdata[1]
> pdata
ID age treatment status
1 1 25 type1 poor
2 2 34 type2 stable
3 3 28 type3 poor
4 4 72 type1 improve
> summary(pdata)
ID age treatment
Min. :1.00 Min. :25.00 Length:4
1st Qu.:1.75 1st Qu.:27.25 Class :character
Median :2.50 Median :31.00 Mode :character
Mean :2.50 Mean :39.75
3rd Qu.:3.25 3rd Qu.:43.50
Max. :4.00 Max. :72.00
status
Length:4
Class :character
Mode :character
我们想按照status对患者进行分类,所以将status由字符串改为factor
> pdata$status <- as.factor(pdata$status)
> class(pdata$status)
[1] "factor"
> summary(pdata)
ID age treatment status
Min. :1.00 Min. :25.00 Length:4 poor :2
1st Qu.:1.75 1st Qu.:27.25 Class :character stable:1
Median :2.50 Median :31.00 Mode :character improve:1
Mean :2.50 Mean :39.75
3rd Qu.:3.25 3rd Qu.:43.50
Max. :4.00 Max. :72.00
status是一个因子向量,表明了每个患者的病情是恶化、稳定、好转,类别总共有三大类。
因为通过factor对患者进行了分类,所以可以对不同类别进行作图:
> library(ggplot2)
> ggplot(data = pdata, aes(x = status)) + geom_bar()
levels()函数
对factor进行排序、增删,有利于清洗数据、作图、比较
> levels(pdata$status)
[1] "improve" "poor" "stable"
批量修改其中某一类别患者的status,如把improve改为stable
> pdata$status[pdata$status=="improve"] <-"stable"
> pdata
ID age treatment status
1 1 25 type1 poor
2 2 34 type2 stable
3 3 28 type3 poor
4 4 72 type1 stable
此时检查还有哪些levels
> levels(pdata$status)
[1] "improve" "poor" "stable"
improve这一类还在。如果我们想要删掉这一类别,则droplevels()
> pdata$status <- droplevels(pdata$status)
> levels(pdata$status)
[1] "poor" "stable"
此时没有对应患者的status被删掉
几种对levels的赋值方法
levels(f1)
levels(f2) <- value
attr(x, "levels") <- value
gl()函数:generate factor levels, 生成因子levels
gl(n,k,labels = c(), ordered = T)
n: 有几类
k: 每类重复几个
labels: 每类起什么名字
ordered:是否排序
> ## assign individual levels
> x <- gl(2, 4, 8)
> levels(x)[1] <- "low"
> levels(x)[2] <- "high"
> x
[1] low low low low high high high high
Levels: low high
> ## or as a group
> y <- gl(2, 4, 8)
> levels(y) <- c("low", "high")
> y
[1] low low low low high high high high
Levels: low high
将某几类归为一类
> ## combine some levels
> z <- gl(3, 2, 12, labels = c("apple", "salad", "orange"))
> z
[1] apple apple salad salad orange orange apple apple salad
[10] salad orange orange
Levels: apple salad orange
> levels(z) <- c("fruit", "veg", "fruit")
> z
[1] fruit fruit veg veg fruit fruit fruit fruit veg veg fruit fruit
Levels: fruit veg
重复出现的类别归为一类
> ## same, using a named list
> z <- gl(3, 2, 12, labels = c("apple", "salad", "orange"))
> z
[1] apple apple salad salad orange orange apple apple salad
[10] salad orange orange
Levels: apple salad orange
> levels(z) <- list("fruit" = c("apple","orange"), "veg" = "salad")
> z
[1] fruit fruit veg veg fruit fruit fruit fruit veg veg
[11] fruit fruit
Levels: fruit veg
加入data.frame中没有出现的组别
> ## we can add levels this way:
> f <- factor(c("a","b"))
> levels(f) <- c("c", "a", "b")
> f
[1] c a
Levels: c a b
对组别的命名
> f <- factor(c("a","b"))
> levels(f) <- list(C = "C", A = "a", B = "b")
> f
[1] A B
Levels: C A B
将数值类型转换成因子
直接用as.numeric转换会有问题,转换后的内容不是你想要的:
> f <- factor(c(3.4,1.2,5))
> f
[1] 3.4 1.2 5
Levels: 1.2 3.4 5
> as.numeric(f)
[1] 2 1 3
正确的方式是
> f <- factor(c(3.4,1.2,5))
> f <- levels(f)[f]
> f <- as.numeric(f)
> f
[1] 3.4 1.2 5.0