tidyverse odds & ends
RNA Bioscience Initiative | CU Anschutz
2024-10-21
[
, [[
, and $
[1] 110 110 93 110 175 105 245 62 95 123 123 180 180 180 205 215 230 66 52
[20] 65 97 150 150 245 175 66 91 113 264 175 335 109
hp mpg
Mazda RX4 110 21.0
Mazda RX4 Wag 110 21.0
Datsun 710 93 22.8
Hornet 4 Drive 110 21.4
Hornet Sportabout 175 18.7
Valiant 105 18.1
Duster 360 245 14.3
Merc 240D 62 24.4
Merc 230 95 22.8
Merc 280 123 19.2
Merc 280C 123 17.8
Merc 450SE 180 16.4
Merc 450SL 180 17.3
Merc 450SLC 180 15.2
Cadillac Fleetwood 205 10.4
Lincoln Continental 215 10.4
Chrysler Imperial 230 14.7
Fiat 128 66 32.4
Honda Civic 52 30.4
Toyota Corolla 65 33.9
Toyota Corona 97 21.5
Dodge Challenger 150 15.5
AMC Javelin 150 15.2
Camaro Z28 245 13.3
Pontiac Firebird 175 19.2
Fiat X1-9 66 27.3
Porsche 914-2 91 26.0
Lotus Europa 113 30.4
Ford Pantera L 264 15.8
Ferrari Dino 175 19.7
Maserati Bora 335 15.0
Volvo 142E 109 21.4
[
can return a range, [[
returns a single value.
one-step filtering.
[1] 110 110 110 175 105 245 123 123 180 180 180 205 215 230 150 150 245 175 113
[20] 264 175 335 109
two-step filtering. same result.
[1] TRUE TRUE FALSE TRUE TRUE TRUE TRUE FALSE FALSE TRUE TRUE TRUE
[13] TRUE TRUE TRUE TRUE TRUE FALSE FALSE FALSE FALSE TRUE TRUE TRUE
[25] TRUE FALSE FALSE TRUE TRUE TRUE TRUE TRUE
also can use with is.na()
to identify / exclude NA
values in a vector.
Use sum()
to figure out how many are TRUE
.
stringr provides several useful functions for operating on strings.
See the stringr cheatsheet
forcats provides several utilities for working with factors.
See the forcats cheatsheet
# A tibble: 344 × 3
species island bill_length_mm
<fct> <fct> <dbl>
1 Adelie Torgersen 39.1
2 Adelie Torgersen 39.5
3 Adelie Torgersen 40.3
4 Adelie Torgersen NA
5 Adelie Torgersen 36.7
6 Adelie Torgersen 39.3
7 Adelie Torgersen 38.9
8 Adelie Torgersen 39.2
9 Adelie Torgersen 34.1
10 Adelie Torgersen 42
# ℹ 334 more rows
# A tibble: 3 × 2
f n
<fct> <int>
1 Adelie 152
2 Chinstrap 68
3 Gentoo 124
bind_cols()
left_join()
right_join()
inner_join()
full_join()
bind_rows()
intersect()
setdiff()
union()
Look at “combine variables” and “combine cases” at the top.
# A tibble: 3 × 3
name band plays
<chr> <chr> <chr>
1 Mick Stones <NA>
2 John Beatles guitar
3 Paul Beatles bass
# A tibble: 2 × 2
name band
<chr> <chr>
1 John Beatles
2 Paul Beatles
There are many other dplyr verbs.
rename
, count
, add_row
, add_column
, distinct
, sample_n
, sample_frac
, slice
, pull
Check out the dplyr cheatsheet to learn more!
scale_color_brewer()
and scale_fill_brewer()
control color
and fill
aesthetics.Yes, but it is not exactly straightforward.
# fetch the legend for `p1`
legend <- get_legend(
p + theme(legend.position = "bottom")
)
p <- p + theme(legend.position = "none")
# first `plot_grid` builds the panels
panels <- plot_grid(
p, p, p, p,
labels = c(
"A", "B", "C", "D"
),
nrow = 2
)
# second `plot_grid` adds the legend to the panels
plot_grid(
panels,
legend,
ncol = 1,
rel_heights = c(1, .1)
)
Saves last plot as 5’ x 5’ file named plot_final.png
in working directory.
Matches file type to file extension.
Course website: https://rnabioco.github.io/molb-7950