Factors

James Normington

Announcements

  • No class Thursday – MSCS Capstone Days
    • Assignment 7 – attend two talks and write a reflection. Description on course schedule.
  • Midterm next Thursday!
    • Link to description/study guide on course schedule

    • Next Tuesday we will review

Template File

Download a template .Rmd of this activity. Put the file in a Day_10 folder within your COMP_STAT_112 folder.

  • This .Rmd only contains 1 exercise
  • Submit today’s activity as Assignment 6 Part 2

Factors vs. Characters

Both: can represent categorical variables

character: does not have predefined levels

  • Names

  • Addresses

  • Free responses (e.g., How does this class make you feel?)

Factors vs. Characters

R class factor: does have predefined levels, better for categorical analysis:

  • Final grade: A, A-, B+, B….

  • Favorite season: winter, spring, summer, autumn

  • Multiple choice response (e.g., How do you feel about this class? love, like, neutral, dislike, hate)

Factors vs. Characters in R

Variables with class character do not have levels

library(readr); library(dplyr)
Grades <- read_csv("https://jamesnormington.github.io/112_spring_2023/data/grades.csv")

head(Grades)
# A tibble: 6 × 3
  sid    grade sessionID  
  <chr>  <chr> <chr>      
1 S31185 D+    session1784
2 S31185 B+    session1785
3 S31185 A-    session1791
4 S31185 B+    session1792
5 S31185 B-    session1794
6 S31185 C+    session1795
class(Grades$grade)
[1] "character"
levels(Grades$grade)
NULL

Converting a character to factor

Grades = Grades %>%
  mutate(grade = factor(grade))

levels(Grades$grade)
 [1] "A"  "A-" "AU" "B"  "B-" "B+" "C"  "C-" "C+" "D"  "D-" "D+" "NC" "S" 

Changing the order of levels

  1. fct_relevel(): manually reorder levels
  2. fct_infreq(): order levels from higher to lowest frequency
  3. fct_reorder(): reorder levels by values of another variable
  4. fct_rev(): reverse current order

Changing the value of levels

  1. fct_recode(): manually change levels

  2. fct_lump(): group together least common levels

Work time

Work on the short Factor examples, then go on to Assignment 6 or study for Midterm.

  1. Work in groups!
  2. Ask me questions!