Announcements
- No class Thursday – MSCS Capstone Days
- Assignment 7 – attend two talks and write a reflection. Description on course schedule.
- Midterm next Thursday!
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
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 level
s
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
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
fct_relevel()
: manually reorder levels
fct_infreq()
: order levels from higher to lowest frequency
fct_reorder()
: reorder levels by values of another variable
fct_rev()
: reverse current order
Changing the value of levels
fct_recode():
manually change levels
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.
- Work in groups!
- Ask me questions!