R Programming
  • The wikipedia of R by me
  • Hello R
    • -What is R & RStudio
    • -Learning sources
    • -R online editor
    • -R environment
  • Data types
    • -Dealing with Number
    • -Dealing with String
    • -Dealing with Dates
    • -Dealing with NA's
    • -Dealing with Logicals
    • -Dealing with Factors
  • R data
    • -R object
    • -Data structures
      • --Basics
      • --Managing Vectors
      • --Managing Matrices
      • --Managing Data Frames
    • -Functions
    • -Importing/exporting data
    • -Shape&Transform data
    • -R management
  • Visualizations
  • Intro to R Bootcamp
    • -01-introduction
    • -02-data preparation
    • -03-data transformation
    • -04-visualization
  • R programming track
    • -a-Introduction to R
      • --1-Intro to basics
      • --2-Vectors
      • --3-Matrices
      • --4-Factors
      • --5-Data frames
      • --6-Lists
    • -b-Intermediate R
      • --1-Conditionals and Control Flow
      • --2-Loops
      • --3-Functions
      • --4-The apply family
      • --5-Utilities
    • -d-Writing Functions in R
      • --1-A quick refresher
      • --2-When and how you should write a function
      • --3-Functional programming
      • --4-Advanced inputs and outputs
      • --5-Robust functions
  • Data Wrangling with R
  • R-tutor
    • #R introduction
    • #Elementary Statistics with R
  • Hands-On Programming with R
  • R for Data Science
  • Advanced R
  • ggplot2
  • R packages
  • Statistik-1
  • Statistik-2
  • Statistik-3
  • Zeitreihen & Prognosen
  • Descriptive Analytics
  • Predictive Analytics
  • Prescriptive Analytics
  • R Graphics Cookbook
    • ggplot2 intro
    • ggplot2 custome
    • ggplot top-50
  • #Exploratory Data Analysis
    • -Data Summary
    • -Checklist Solution
  • #Data Mining
    • Untitled
    • Untitled
  • #Machine Learning
    • Intro to ML
    • Intro alghorithms
    • 1. Supervised Learning
  • Master R for Data Science
    • Learning R
    • Untitled
    • Untitled
  • Data Science Projects
    • Simple linear regression:
Powered by GitBook
On this page
  1. R programming track
  2. -a-Introduction to R

--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
Previous--3-MatricesNext--5-Data frames

Last updated 6 years ago