R 的類別資料
前面我們曾經使用 str 函數查看 Squid 的資料欄位資訊:
str(Squid)
'data.frame': 2644 obs. of 6 variables: $ Sample : int 1 2 3 4 5 6 7 8 9 10 ... $ Year : int 1 1 1 1 1 1 1 1 1 1 ... $ Month : int 1 1 1 1 1 1 1 1 1 2 ... $ Location: int 1 3 1 1 1 1 1 3 3 1 ... $ Sex : int 2 2 2 2 2 2 2 2 2 2 ... $ GSI : num 10.44 9.83 9.74 9.31 8.99 ...
其中 Location 是以 1、2、3 或 4 來表示,而 Sex 則是使用 1 與 2 來表示,這樣的資料都是屬於典型的類別資料,若是在 Excel 中,我們可以很容易地將 Sex 寫成 male 與 female,這樣可以更容易辨識資料所代表的意義,在 R 中也可以做類似的處理,將類些資料轉為類別性資料:
Squid$fLocation <- factor(Squid$Location) Squid$fSex <- factor(Squid$Sex)
R 的 factor 是專門用來儲存類別性資料的一種變數型態,我們利用 factor 函數來將 Location 與 Sex 轉為 factor,儲存在新的資料表欄位中,我們可以看一下 fSex 這個 factor 的資料:
Squid$fSex
輸出為
[1] 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 [19] 2 2 2 2 2 1 2 2 2 2 2 2 2 2 2 2 2 2 [37] 2 2 2 2 2 2 2 2 2 2 2 1 2 2 2 2 2 2 [略] [2611] 1 2 1 1 2 1 1 2 1 2 2 1 1 1 1 1 1 1 [2629] 1 1 2 1 1 1 2 1 2 1 2 1 2 1 1 1 Levels: 1 2
最後一行的 Levels: 1 2 表示 fSex 有兩種類別,分別為 1 與 2,而 factor 內部的類別名稱是可以修改的:
Squid$fSex <- factor(Squid$Sex, levels = c(1, 2), labels = c("M", "F")) Squid$fSex
輸出為:
[1] F F F F F F F F F F F F F F F F F F [19] F F F F F M F F F F F F F F F F F F [37] F F F F F F F F F F F M F F F F F F [略] [2611] M F M M F M M F M F F M M M M M M M [2629] M M F M M M F M F M F M F M M M Levels: M F
如此一來,所有的 1 就會以 M 表示,而 2 就會以 F 表示。
前面我們介紹過用簡單的判斷式篩選資料的方法:
SquidM <- Squid[Squid$Sex == 1, ]
而如果要使用 factor 的欄位進行篩選,輸入類別名稱時,記得要加上引號:
SquidM <- Squid[Squid$fSex == "M", ]
在 R 中的許多函數都可以直接讀取 factor 的資料,例如畫 box plot:
boxplot(GSI ~ fSex, data = Squid)
回歸分析也可以使用 factor 的變數:
M1 <- lm(GSI ~ fSex + fLocation, data = Squid) summary(M1)
輸出為:
Call:
lm(formula = GSI ~ fSex + fLocation, data = Squid)
Residuals:
Min 1Q Median 3Q Max
-3.4137 -1.3195 -0.1593 1.2039 11.2159
Coefficients:
Estimate Std. Error t value
(Intercept) 1.35926 0.07068 19.230
fSexF 2.02481 0.09427 21.479
fLocation2 -1.85525 0.20027 -9.264
fLocation3 -0.14248 0.12657 -1.126
fLocation4 0.58756 0.34934 1.682
Pr(>|t|)
(Intercept) <2e-16 ***
fSexF <2e-16 ***
fLocation2 <2e-16 ***
fLocation3 0.2604
fLocation4 0.0927 .
---
Signif. codes:
0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05
‘.’ 0.1 ‘ ’ 1
Residual standard error: 2.415 on 2639 degrees of freedom
Multiple R-squared: 0.1759, Adjusted R-squared: 0.1746
F-statistic: 140.8 on 4 and 2639 DF, p-value: < 2.2e-16
M2 <- lm(GSI ~ factor(Sex) + factor(Location), data = Squid) summary(M2)
輸出為:
Call:
lm(formula = GSI ~ factor(Sex) + factor(Location), data = Squid)
Residuals:
Min 1Q Median 3Q Max
-3.4137 -1.3195 -0.1593 1.2039 11.2159
Coefficients:
Estimate Std. Error
(Intercept) 1.35926 0.07068
factor(Sex)2 2.02481 0.09427
factor(Location)2 -1.85525 0.20027
factor(Location)3 -0.14248 0.12657
factor(Location)4 0.58756 0.34934
t value Pr(>|t|)
(Intercept) 19.230 <2e-16 ***
factor(Sex)2 21.479 <2e-16 ***
factor(Location)2 -9.264 <2e-16 ***
factor(Location)3 -1.126 0.2604
factor(Location)4 1.682 0.0927 .
---
Signif. codes:
0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05
‘.’ 0.1 ‘ ’ 1
Residual standard error: 2.415 on 2639 degrees of freedom
Multiple R-squared: 0.1759, Adjusted R-squared: 0.1746
F-statistic: 140.8 on 4 and 2639 DF, p-value: < 2.2e-16
除了 factor 函數之外,我們也可以使用 as.factor 這個函數將資料轉為 factor,而如果要將 factor 轉為數值,可以使用 as.numeric 函數,這個在畫圖的時候,要將不同類別的資料以不同顏色表示時,會很好用。
fLocation 的內容也是類似:
Squid$fLocation
輸出為
[1] 1 3 1 1 1 1 1 3 3 1 1 1 1 1 1 1 3 1 [19] 3 1 3 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 [37] 1 1 1 3 1 1 1 1 3 1 1 3 1 1 1 1 1 1 [略] [2611] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 [2629] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 Levels: 1 2 3 4
fLocation 有四個 levels,順序是由小到大,許多繪圖函數在使用 factor 時,會受到這個順序的影響,例如 boxplot 畫出來的圖形就是依照這個順序來排列的,必要的時候我們可以自行指定這個順序:
Squid$fLocation <- factor(Squid$Location, levels = c(2, 3, 1, 4)) Squid$fLocation
輸出為:
[1] 1 3 1 1 1 1 1 3 3 1 1 1 1 1 1 1 3 1 [19] 3 1 3 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 [37] 1 1 1 3 1 1 1 1 3 1 1 3 1 1 1 1 1 1 [略] [2611] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 [2629] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 Levels: 2 3 1 4
改變 levels 的順序並不會影響實際的資料,不過對於 boxplot 這類的繪圖函數就會有一些差別:
boxplot(GSI ~ fLocation, data = Squid)
這樣畫出來的圖就會依照我們指定的順序來排列。
Exercise 4
接續使用 Exercise 2 的資料,進行下列步驟:
- 將
Month與Year轉為 factor 後建立兩個新的欄位fMonth與fYear。 - 使用新的
fYear欄位篩選出 2002 年所有的資料。
山崎退
新手發問~~小弟沒有資訊背景,剛接觸R不久
請問文章開頭的資料檔squid.txt是需自行建立嗎?
或者是有載點可下載
蠻希望跟著板主的文章來練習
若有不妥還請包涵~~感謝!
G. T. Wang
是我忘記放下載連結,真是抱歉!
我已經將下載連結放在文章中了,如果還有其他問題請再跟我說,謝謝您。