(I am working in R 3.02 OSX.)
I have a data set where the variable of interest is imported as an int
with values of 1, 2, 3 and they represent educational level.
Before conversion it was
str(df$var)
int [1:5000] 1 1 1 2 2 2 2 3 2 3 ...
I tried to create an ordered factor:
var_name <- ordered(df$var, levels = c(1,2,3),
labels = c("Undergraduate Degree", "Graduate Degree",
"Professional Degree"))
The variable is changed to a ordered factor with three levels (I see that in the str
function). However all the values are now gone and replaced with all NA
s.
I am able to get what I want by simply changing ordered()
to factor()
:
var_name <- factor(df$var, levels = c(1,2,3),
labels = c("Undergraduate Degree", "Graduate Degree",
"Professional Degree"))
However, I am curious why does ordered()
remove all the values? What did I do wrong in that function?