STAT 20: Introduction to Probability and Statistics
name house height spells
1 Harry Gryffindor 1.78 60
2 Bellatrix Slytherin 1.57 75
3 Hermione Gryffindor 1.65 70
4 Draco Slytherin 1.75 55
name house height spells
1 Harry Gryffindor 1.78 60
2 Bellatrix Slytherin 1.57 75
3 Hermione Gryffindor 1.65 70
4 Draco Slytherin 1.75 55
Goal: Calculate the average height of characters from Gryffindor
Let’s look at three ways to solve this.
Must be read from the inside out
Hard to keep track of arguments
mean(height)
1 1.715
Have to repeat data frame names
Creates unnecessary objects
mean(height)
1 1.715
Can be read like an English paragraph
Only type the data once
No leftover objects
It’s good practice to understand the output of each line by breaking the pipe.
name house height spells
1 Harry Gryffindor 1.78 60
2 Bellatrix Slytherin 1.57 75
3 Hermione Gryffindor 1.65 70
4 Draco Slytherin 1.75 55
Calculate the average height of characters across each house.
group_by()
Flag the rows of a data frame as belong to a group defined by a factor. For use in downstream operations.
group_by()
Flag the rows of a data frame as belong to a group defined by a factor. For use in downstream operations.
group_by()
with summarize()
group_by()
with summarize()
# A tibble: 2 × 2
house `mean(height)`
<chr> <dbl>
1 Gryffindor 1.72
2 Slytherin 1.66
group_by()
with filter()
group_by()
with filter()
# A tibble: 2 × 4
# Groups: house [2]
name house height spells
<chr> <chr> <dbl> <dbl>
1 Harry Gryffindor 1.78 60
2 Draco Slytherin 1.75 55
group_by()
with arrange()
group_by()
with arrange()
# A tibble: 4 × 4
# Groups: house [2]
name house height spells
<chr> <chr> <dbl> <dbl>
1 Harry Gryffindor 1.78 60
2 Draco Slytherin 1.75 55
3 Hermione Gryffindor 1.65 70
4 Bellatrix Slytherin 1.57 75
arrange()
ignores group_by()
and is always global.
group_by()
with mutate()
group_by()
with mutate()
# A tibble: 4 × 5
# Groups: house [2]
name house height spells height_z
<chr> <chr> <dbl> <dbl> <dbl>
1 Harry Gryffindor 1.78 60 0.707
2 Bellatrix Slytherin 1.57 75 -0.707
3 Hermione Gryffindor 1.65 70 -0.707
4 Draco Slytherin 1.75 55 0.707