[^aeoui] will match things like “1” or “$”left_join and right_join are the most commonfull_join is rare: for messy datasets or data quality checksinner_join only when you want to also filter dataanti_join usually to search for missing valuessemi_join when you don’t need the dataanti_join missing valuesreturn statement also[1] 1.2e+08
[1] 3.1415 2.0000 -100.0000
[1] 1 1 NA
[1] NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA
[26] NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA
[51] NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA
[76] NA NA NA NA
If you find yourself repeating code more than a few times, consider turning it into a function
<<- operator:Error in `group_by()`:
! Must group by variables found in `.data`.
✖ Column `group_var` is not found.
When group_by and summarize are called, they treat group_var and mean_var literally as names of variables to search for in data
Called data masking because real values of group_var and mean_var ignored
df <- tibble(
mean_var = 1,
group_var = "g",
group = 1,
x = 10,
y = 100
)
df |> grouped_mean(group, x)# A tibble: 1 × 2
group_var `mean(mean_var)`
<chr> <dbl>
1 g 1
# A tibble: 1 × 2
group_var `mean(mean_var)`
<chr> <dbl>
1 g 1
Fundamental Theorem of Software Engineering:
“We can solve any problem by introducing an extra layer of indirection”
An indirection or reference is a way to refer to something using a name, reference, or container, instead of the value itself
R expects name to be typed in
embrace the variable: filter(df, {{var}} == cond)
library(nycflights13)
count_missing <- function(df, group_vars, x_var) {
df |>
group_by({{ group_vars }}) |>
summarize(
n_miss = sum(is.na({{ x_var }})),
.groups = "drop"
)
}
flights |>
count_missing(c(year,month,day), dep_time)Error in `group_by()`:
ℹ In argument: `c(year, month, day)`.
Caused by error:
! `c(year, month, day)` must be size 336776 or 1, not 1010328.
pick lets you tidy selectcount_missing <- function(df, group_vars, x_var) {
df |>
group_by(pick({{ group_vars }})) |>
summarize(
n_miss = sum(is.na({{ x_var }})),
.groups = "drop"
)
}
flights |>
count_missing(c(year,month,day), dep_time) |> head(6)# A tibble: 6 × 4
year month day n_miss
<int> <int> <int> <int>
1 2013 1 1 4
2 2013 1 2 8
3 2013 1 3 10
4 2013 1 4 6
5 2013 1 5 3
6 2013 1 6 1
R looks more functional than other languagespurrr library provides functional programming toolsacross iterates on columnsSuppose we want to apply an operation to many columns at once:
# A tibble: 10 × 4
a b c d
<dbl> <dbl> <dbl> <dbl>
1 -0.333 0.408 0.103 -1.39
2 1.21 -0.647 -1.17 -1.76
3 -0.599 0.166 -0.0823 -0.976
4 -0.188 0.913 -1.54 -0.439
5 -0.350 -0.146 1.50 0.287
6 0.0939 1.53 0.957 0.353
7 -2.06 0.996 -0.676 1.26
8 -0.265 -0.644 -0.398 0.206
9 0.676 0.136 -1.24 1.15
10 1.18 0.293 -1.44 -0.883
across iterates on columns# A tibble: 1 × 5
n a b c d
<int> <dbl> <dbl> <dbl> <dbl>
1 10 -0.0631 0.300 -0.399 -0.220
across iterates on columnsacrosspurrr functional programming library# A tibble: 1 × 5
n a b c d
<int> <dbl> <dbl> <dbl> <dbl>
1 10 -0.0631 0.300 -0.399 -0.220
starts_with, everything)acrossacrossmap family in purrrmap(vec,f) = [f(vec[1]),f(vec[2]),....] map family in purrrmap(vec,f) = [f(vec[1]),f(vec[2]),....] 
map returns a list, not a vector
R functions do not have strict return types
map_*map_dbl, map_chr, map_lgl, map_int: assume data type and return vectorsmap_* mpg cyl disp hp drat wt qsec vs am gear carb
Mazda RX4 21.0 6 160.0 110 3.90 2.620 16.46 0 1 4 4
Mazda RX4 Wag 21.0 6 160.0 110 3.90 2.875 17.02 0 1 4 4
Datsun 710 22.8 4 108.0 93 3.85 2.320 18.61 1 1 4 1
Hornet 4 Drive 21.4 6 258.0 110 3.08 3.215 19.44 1 0 3 1
Hornet Sportabout 18.7 8 360.0 175 3.15 3.440 17.02 0 0 3 2
Valiant 18.1 6 225.0 105 2.76 3.460 20.22 1 0 3 1
Duster 360 14.3 8 360.0 245 3.21 3.570 15.84 0 0 3 4
Merc 240D 24.4 4 146.7 62 3.69 3.190 20.00 1 0 4 2
mpg cyl disp hp drat wt qsec vs am gear carb
25 3 27 22 22 29 30 2 2 3 6
map functionfor loops?for and while loops exist in Rmapwhile loop in actionPlease fill out the following google form after the meeting or watching the video: