0

I have factor variables in a data frame. I want some of them to be ordered (such as education) and some of them unordered (such as sex).

I create some of the variables and some get created automatically.

Is there a way to check that? Maybe using str() or some other method?

ThomasIsCoding
  • 96,636
  • 9
  • 24
  • 81
itay friedman
  • 37
  • 1
  • 7

3 Answers3

1

So after long searched i realize it simply answer all my factor was unordered. ordered factor sign by greater then sign (>) and unordered there is no sign between levels as shown in this code

> 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

one web page that help me understand was http://www.ats.ucla.edu/stat/r/modules/factor_variables.htm

itay friedman
  • 37
  • 1
  • 7
0

You can simply use this for checking class of each variable for 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"
leoluyi
  • 942
  • 1
  • 7
  • 14
0

The function is.ordered() returns TRUE when its argument is an ordered factor and FALSE otherwise.

Following the example by @leouyi:

iris$Sepal.Length <- is.ordered(iris$Sepal.Length)

sapply(iris, is.ordered)

Sepal.Length  Sepal.Width Petal.Length  Petal.Width      Species    
        TRUE        FALSE        FALSE        FALSE        FALSE 
jorvaor
  • 73
  • 1
  • 2
  • 10