04:00
Application Exercise
"load-packages"
starwars
data04:00
Extract rows with filter()
|
|
Extract columns with select()
|
|
Arrange/sort rows with arrange()
|
|
Make new columns with mutate()
|
|
Make group summaries withgroup_by() |> summarize()
|
filter()
filter()
Extract rows that meet some sort of test
filter(.data = DATA, ...)
DATA
= Data frame to transform...
= One or more tests filter()
returns each row for which the test is TRUEfilter(.data = starwars, species == "Droid")
name | species | height |
---|---|---|
Luke Skywalker | Human | 172 |
C-3PO | Droid | 167 |
R2-D2 | Droid | 96 |
Darth Vader | Human | 202 |
Leia Organa | Human | 150 |
… | … | … |
filter(.data = starwars, species == "Droid")
name | species | height |
---|---|---|
Luke Skywalker | Human | 172 |
C-3PO | Droid | 167 |
R2-D2 | Droid | 96 |
Darth Vader | Human | 202 |
Leia Organa | Human | 150 |
… | … | … |
name | species | height |
---|---|---|
C-3PO | Droid | 167 |
R2-D2 | Droid | 96 |
R5-D4 | Droid | 97 |
IG-88 | Droid | 200 |
R4-P17 | Droid | 96 |
BB8 | Droid | NA |
filter()
Test | Meaning | Test | Meaning |
---|---|---|---|
x < y
|
Less than |
x %in% y
|
In (group membership) |
x > y
|
Greater than |
is.na(x)
|
Is missing |
==
|
Equal to |
!is.na(x)
|
Is not missing |
x <= y
|
Less than or equal to | ||
x >= y
|
Greater than or equal to | ||
x != y
|
Not equal to |
Application Exercise
In the Console, use filter()
and logical tests to show…
04:00
filter()
with multiple conditionsExtract rows that meet every test
filter(.data = starwars, species == "Droid", height > 100)
name | species | height |
---|---|---|
Luke Skywalker | Human | 172 |
C-3PO | Droid | 167 |
R2-D2 | Droid | 96 |
Darth Vader | Human | 202 |
Leia Organa | Human | 150 |
… | … | … |
filter(.data = starwars, species == "Droid", height > 100)
name | species | height |
---|---|---|
Luke Skywalker | Human | 172 |
C-3PO | Droid | 167 |
R2-D2 | Droid | 96 |
Darth Vader | Human | 202 |
Leia Organa | Human | 150 |
… | … | … |
name | species | height |
---|---|---|
C-3PO | Droid | 167 |
IG-88 | Droid | 200 |
Operator | Meaning |
---|---|
a & b
|
and |
a | b
|
or |
!a
|
not |
Application Exercise
Create a new R chunk in your .qmd file. Use filter()
and Boolean logical tests to show…
04:00
👎
👍
VERB(DATA, ...)
VERB
= dplyr function/verbDATA
= Data frame to transform...
= Stuff the verb doesmutate()
mutate(.data, ...)
DATA
= Data frame to transform...
= Columns to makemutate(.data = starwars, bmi = mass / (height / 100)^2)
name | mass | height |
---|---|---|
Luke Skywalker | 77 | 172 |
C-3PO | 75 | 167 |
R2-D2 | 32 | 96 |
Darth Vader | 136 | 202 |
Leia Organa | 49 | 150 |
… | … | … |
mutate(.data = starwars, bmi = mass / (height / 100)^2)
name | mass | height |
---|---|---|
Luke Skywalker | 77 | 172 |
C-3PO | 75 | 167 |
R2-D2 | 32 | 96 |
Darth Vader | 136 | 202 |
Leia Organa | 49 | 150 |
… | … | … |
name | … | bmi |
---|---|---|
Luke Skywalker | … | 26.03 |
C-3PO | … | 26.89 |
R2-D2 | … | 34.72 |
Darth Vader | … | 33.33 |
Leia Organa | … | 21.78 |
… | … | … |
mutate(.data = starwars,
height_m = height / 100,
bmi = mass / (height_m)^2)
name | mass | height |
---|---|---|
Luke Skywalker | 77 | 172 |
C-3PO | 75 | 167 |
R2-D2 | 32 | 96 |
Darth Vader | 136 | 202 |
Leia Organa | 49 | 150 |
… | … | … |
mutate(.data = starwars,
height_m = height / 100,
bmi = mass / (height_m)^2)
name | mass | height |
---|---|---|
Luke Skywalker | 77 | 172 |
C-3PO | 75 | 167 |
R2-D2 | 32 | 96 |
Darth Vader | 136 | 202 |
Leia Organa | 49 | 150 |
… | … | … |
name | … | height_m | bmi |
---|---|---|---|
Luke Skywalker | … | 1.72 | 26.03 |
C-3PO | … | 1.67 | 26.89 |
R2-D2 | … | 0.96 | 34.72 |
Darth Vader | … | 2.02 | 33.33 |
Leia Organa | … | 1.5 | 21.78 |
… | … | … | … |
ifelse()
mutate()
ifelse(TEST,
VALUE_IF_TRUE,
VALUE_IF_FALSE)
TEST
= A logical testVALUE_IF_TRUE
= What happens if test is trueVALUE_IF_FALSE
= What happens if test is falseApplication Exercise
Create a new R chunk in your .qmd file. Use mutate()
to
droid
column that is TRUE
if the species is a Droidlog()
)human_droid
column that says “Human or Droid” if the character is a human or a droid and “Not Human or Droid” if it’s not05:00
. . .
The |>
operator (pipe) takes an object on the left and passes it as the first argument of the function on the right
. . .
starwars |> filter(_, species == "Droid")
filter(starwars, species == "Droid")
starwars |> filter(species == "Droid")
|>
summarize()
name | species | height |
---|---|---|
Luke Skywalker | Human | 172 |
C-3PO | Droid | 167 |
R2-D2 | Droid | 96 |
Darth Vader | Human | 202 |
Leia Organa | Human | 150 |
… | … | … |
summarize()
name | species | height |
---|---|---|
Luke Skywalker | Human | 172 |
C-3PO | Droid | 167 |
R2-D2 | Droid | 96 |
Darth Vader | Human | 202 |
Leia Organa | Human | 150 |
… | … | … |
mean_height |
---|
174.358 |
00:30
summarize()
name | species | height |
---|---|---|
Luke Skywalker | Human | 172 |
C-3PO | Droid | 167 |
R2-D2 | Droid | 96 |
Darth Vader | Human | 202 |
Leia Organa | Human | 150 |
… | … | … |
mean_height | max_height |
---|---|
174.358 | 264 |
Application Exercise
Create a new R chunk in your .qmd file. Use summarize()
to calculate…
06:00
Application Exercise
Create a new R chunk in your .qmd file. Use filter()
and summarize()
to calculate
05:00
group_by()
. . .
. . .
summarize()
Application Exercise
Create a new R chunk in your .qmd file.
06:00
Slides adapted from Andrew Heiss by Dr. Lucy D’Agostino McGowan