簡體   English   中英

如何檢查R中的順序因子變量

[英]How to check order factor variable in R

我在數據框中有因子變量。 我希望其中一些是有序的(例如教育),而其中一些是無序的(例如性)。

我創建了一些變量,有些是自動創建的。

有沒有辦法檢查? 也許使用str()或其他一些方法?

所以經過長時間的搜索,我意識到它只是回答我的所有因素都是無序的。 有序因子符號大於符號 (>) 和無序,級別之間沒有符號,如此代碼所示

> orderEduc[1:6]
[1] high        junior-high high        elementary  high        junior-high
Levels: elementary < junior-high < high < academic

> finalDF$eduction[1:6]
[1] high        junior-high high        elementary  high        junior-high
Levels: elementary junior-high high academic

一個幫助我理解的網頁是http://www.ats.ucla.edu/stat/r/modules/factor_variables.htm

您可以簡單地使用它來檢查data.frame的每個變量的data.frame

iris$Sepal.Length <- as.ordered(iris$Sepal.Length)
sapply(iris, class)

# $Sepal.Length
# [1] "ordered" "factor" 
# 
# $Sepal.Width
# [1] "numeric"
# 
# $Petal.Length
# [1] "numeric"
# 
# $Petal.Width
# [1] "numeric"
# 
# $Species
# [1] "factor"

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM