Factors Flashcards
What is a factor?
A data type used to hold categorical variables.
What is the function to create an unordered factor?
factor()
Ex: factor_sex_vector <- factor(c(“Male”,”Female”,”Female”,”Male”,”Male”))
What is a nominal categorical variable, and what is an ordinal categorical variable?
A nominal variable is a categorical variable without an implied order.
An ordinal variable is a categorical variable with a natural ordering.
What is a factor level?
An individual category, such as “Male”, or “Green”.
Which function lets you change the names of factor levels?
levels()
Ex: levels(factor_vector) <- c(“name1”, “name2”,…)
If you don’t specify the levels of the factor when creating the vector, how will R assign them?
Alphabetically
Which function gives you a quick overview of the contents of a variable?
summary()
Ex: summary(my_var)
What happens when you compare elements of a factor?
If the factor levels are nominal, R returns a warning message, telling you that comparisons are not meaningful.
If the factor levels are ordinal, they are compared by the order they were put in.
What is the function to create an ordered factor?
factor()
Ex: factor(some_vector, ordered = TRUE, levels = c(“slow”, “medium” …))
What are the arguments to factor() when creating an ordered factor, aside from the collection of elements, and what do they do?
The argument ordered determines whether the factors are nominal or ordinal.
The argument levels gives the values of the factor the correct order.