-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata-wrangling-examples-filter.R
61 lines (37 loc) · 1.4 KB
/
data-wrangling-examples-filter.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# Load Packages -----------------------------------------------------------
library(tidyverse)
# Import Data -------------------------------------------------------------
penguins <- read_csv("penguins.csv")
# filter() ----------------------------------------------------------------
# We use filter() to choose a subset of observations.
# We use == to select all observations that meet the criteria.
penguins |>
filter(species == "Adelie")
# We use != to select all observations that don't meet the criteria.
penguins |>
filter(species != "Adelie")
# We can combine comparisons and logical operators.
penguins |>
filter(species == "Adelie" | species == "Chinstrap")
# We can use %in% to collapse multiple comparisons into one.
penguins |>
filter(species %in% c("Adelie", "Chinstrap"))
# We can chain together multiple filter functions.
# Doing it this way, we don't have create complex logic in one line.
# Complicated version
penguins |>
filter(species %in% c("Adelie", "Chinstrap") & island == "Torgersen")
# Simpler version
penguins |>
filter(species %in% c("Adelie", "Chinstrap")) |>
filter(island == "Torgersen")
# We can use <, >, <=, and => for numeric data.
penguins |>
filter(body_mass_g > 4000)
# We can drop NAs with !is.na().
penguins |>
filter(!is.na(sex))
# But the double negative is confusing.
# We can also drop NAs with drop_na().
penguins |>
drop_na(sex)