R | factor 因子

factor 因子

在R中數(shù)據(jù)的分類(lèi)是用因子數(shù)據(jù)類(lèi)型(factor)來(lái)表示的。如性別。

> #數(shù)據(jù)框 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對(duì)患者進(jìn)行分類(lèi),所以將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是一個(gè)因子向量,表明了每個(gè)患者的病情是惡化、穩(wěn)定、好轉(zhuǎn),類(lèi)別總共有三大類(lèi)。

因?yàn)橥ㄟ^(guò)factor對(duì)患者進(jìn)行了分類(lèi),所以可以對(duì)不同類(lèi)別進(jìn)行作圖:

> library(ggplot2)
> ggplot(data = pdata, aes(x = status)) + geom_bar()
image.png

levels()函數(shù)

對(duì)factor進(jìn)行排序、增刪,有利于清洗數(shù)據(jù)、作圖、比較

> levels(pdata$status)
[1] "improve" "poor"    "stable" 

批量修改其中某一類(lèi)別患者的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

此時(shí)檢查還有哪些levels

> levels(pdata$status)
[1] "improve" "poor"    "stable" 

improve這一類(lèi)還在。如果我們想要?jiǎng)h掉這一類(lèi)別,則droplevels()

> pdata$status <- droplevels(pdata$status)
> levels(pdata$status)
[1] "poor"   "stable"

此時(shí)沒(méi)有對(duì)應(yīng)患者的status被刪掉


幾種對(duì)levels的賦值方法

levels(f1)
levels(f2) <- value
attr(x, "levels") <- value

gl()函數(shù):generate factor levels, 生成因子levels
gl(n,k,labels = c(), ordered = T)
n: 有幾類(lèi)
k: 每類(lèi)重復(fù)幾個(gè)
labels: 每類(lèi)起什么名字
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

將某幾類(lèi)歸為一類(lèi)

> ## 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

重復(fù)出現(xiàn)的類(lèi)別歸為一類(lèi)

> ## 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中沒(méi)有出現(xiàn)的組別

> ## 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

對(duì)組別的命名

> f <- factor(c("a","b"))
> levels(f) <- list(C = "C", A = "a", B = "b")
> f
[1] A B
Levels: C A B

將數(shù)值類(lèi)型轉(zhuǎn)換成因子

直接用as.numeric轉(zhuǎn)換會(huì)有問(wèn)題,轉(zhuǎn)換后的內(nèi)容不是你想要的:

> 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
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容