--4-Factors

#1-What's a factor and why would you use it?

# Assign to the variable theory what this chapter is about!
theory<-"factors for categorical variables"

#2-What's a factor and why would you use it? (2)

# Gender vector
gender_vector <- c("Male", "Female", "Female", "Male", "Male")​
# Convert gender_vector to a factor
factor_gender_vector <-factor(gender_vector)​
# Print out factor_gender_vectorfactor_gender_vector

#3-What's a factor and why would you use it? (3)

# Animals
animals_vector <- c("Elephant", "Giraffe", 
"Donkey", "Horse")
factor_animals_vector <- factor(animals_vector)
factor_animals_vector

​# Temperature
temperature_vector <- c("High", "Low", 
"High","Low", "Medium")
factor_temperature_vector <- factor(temperature_vector, 
order = TRUE, 
levels = c("Low", "Medium", "High"))​
factor_temperature_vector

#4-Factor levels

# Code to build factor_survey_vector
survey_vector <- c("M", "F", "F", "M", "M")
factor_survey_vector <- factor(survey_vector)
​# Specify the levels of factor_survey_vector
levels(factor_survey_vector) <-c("Female","Male")
factor_survey_vector​

#5-Summarizing a factor

# Build factor_survey_vector with clean levels
survey_vector <- c("M", "F", "F", "M", "M")
factor_survey_vector <- factor(survey_vector)
levels(factor_survey_vector) <- c("Female", "Male")
factor_survey_vector​

# Generate summary for survey_vector
summary(survey_vector)​

# Generate summary for factor_survey_vector
summary(factor_survey_vector)

#6-Battle of the sexes

#Build factor_survey_vector with clean levels
survey_vector <- c("M", "F", "F", "M", "M")
factor_survey_vector <- factor(survey_vector)
levels(factor_survey_vector) <- c("Female", "Male")

​# Male
male <- factor_survey_vector[1]
male

​# Female
female <- factor_survey_vector[2]
female​

# Battle of the sexes: Male 'larger' than female?
male > female​

#note!
# How interesting! 
By default, 
#R returns NA when you try to compare values in a factor, 
#since the idea doesn't make sense.

#7-Ordered factors

# Create speed_vector
speed_vector <-c("medium","slow","slow","medium","fast")
speed_vector

#8-Ordered factors (2)

# Create speed_vector
speed_vector <-c("medium","slow","slow","medium","fast")

# Convert speed_vector to ordered factor vector
factor_speed_vector <-factor(speed_vector, ordered=TRUE, 
levels=c("slow","medium","fast"))

# Print factor_speed_vector
factor_speed_vector
summary(factor_speed_vector)

#9-Comparing ordered factors

# Create factor_speed_vector
speed_vector <-c("medium","slow","slow","medium","fast")
factor_speed_vector <-factor(speed_vector, ordered=TRUE, 
levels=c("slow","medium","fast"))

# Factor value for second data analyst
da2 <-factor_speed_vector[2]

# Factor value for fifth data analyst
da5 <-factor_speed_vector[5]

# Is data analyst 2 faster than data analyst 5?
da2>da5

Last updated