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?

LyzandeR
  • 35,731
  • 12
  • 70
  • 82
itay friedman
  • 27
  • 1
  • 6

2 Answers2

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
  • 27
  • 1
  • 6
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
  • 852
  • 1
  • 7
  • 14