Download as pdf or txt
Download as pdf or txt
You are on page 1of 17

Data visualization with ggplot2 : : CHEAT SHEET

Basics Geoms Use a geom function to represent data points, use the geom’s aesthetic properties to represent variables.
Each function returns a layer.
ggplot2 is based on the grammar of graphics, the idea
that you can build every graph from the same GRAPHICAL PRIMITIVES TWO VARIABLES
components: a data set, a coordinate system, a <- ggplot(economics, aes(date, unemploy)) both continuous continuous bivariate distribution
and geoms—visual marks that represent data points. b <- ggplot(seals, aes(x = long, y = lat)) e <- ggplot(mpg, aes(cty, hwy)) h <- ggplot(diamonds, aes(carat, price))

F M A a + geom_blank() and a + expand_limits() e + geom_label(aes(label = cty), nudge_x = 1, h + geom_bin2d(binwidth = c(0.25, 500))


Ensure limits include values across all plots. nudge_y = 1) - x, y, label, alpha, angle, color, x, y, alpha, color, fill, linetype, size, weight
+ = b + geom_curve(aes(yend = lat + 1,
family, fontface, hjust, lineheight, size, vjust
h + geom_density_2d()
xend = long + 1), curvature = 1) - x, xend, y, yend, e + geom_point() x, y, alpha, color, group, linetype, size
data geom coordinate plot alpha, angle, color, curvature, linetype, size
x=F·y=A system x, y, alpha, color, fill, shape, size, stroke
a + geom_path(lineend = "butt", h + geom_hex()
To display values, map variables in the data to visual linejoin = "round", linemitre = 1) e + geom_quantile() x, y, alpha, color, fill, size
properties of the geom (aesthetics) like size, color, and x x, y, alpha, color, group, linetype, size x, y, alpha, color, group, linetype, size, weight
and y locations.
a + geom_polygon(aes(alpha = 50)) - x, y, alpha, e + geom_rug(sides = “bl") continuous function
F M A color, fill, group, subgroup, linetype, size x, y, alpha, color, linetype, size i <- ggplot(economics, aes(date, unemploy))

+ = b + geom_rect(aes(xmin = long, ymin = lat,


xmax = long + 1, ymax = lat + 1)) - xmax, xmin,
e + geom_smooth(method = lm)
x, y, alpha, color, fill, group, linetype, size, weight
i + geom_area()
x, y, alpha, color, fill, linetype, size
data geom coordinate plot ymax, ymin, alpha, color, fill, linetype, size
x=F·y=A system e + geom_text(aes(label = cty), nudge_x = 1, i + geom_line()
color = F a + geom_ribbon(aes(ymin = unemploy - 900, nudge_y = 1) - x, y, label, alpha, angle, color,
size = A ymax = unemploy + 900)) - x, ymax, ymin, x, y, alpha, color, group, linetype, size
family, fontface, hjust, lineheight, size, vjust
alpha, color, fill, group, linetype, size
i + geom_step(direction = "hv")
Complete the template below to build a graph. x, y, alpha, color, group, linetype, size
required LINE SEGMENTS
ggplot (data = <DATA> ) + common aesthetics: x, y, alpha, color, linetype, size
one discrete, one continuous visualizing error
<GEOM_FUNCTION> (mapping = aes( <MAPPINGS> ), b + geom_abline(aes(intercept = 0, slope = 1)) f <- ggplot(mpg, aes(class, hwy)) df <- data.frame(grp = c("A", "B"), fit = 4:5, se = 1:2)
stat = <STAT> , position = <POSITION> ) + Not b + geom_hline(aes(yintercept = lat)) j <- ggplot(df, aes(grp, fit, ymin = fit - se, ymax = fit + se))
<COORDINATE_FUNCTION> +
required, b + geom_vline(aes(xintercept = long))
sensible f + geom_col() j + geom_crossbar(fatten = 2) - x, y, ymax,
<FACET_FUNCTION> + defaults b + geom_segment(aes(yend = lat + 1, xend = long + 1)) x, y, alpha, color, fill, group, linetype, size ymin, alpha, color, fill, group, linetype, size
supplied b + geom_spoke(aes(angle = 1:1155, radius = 1))
<SCALE_FUNCTION> +
f + geom_boxplot() j + geom_errorbar() - x, ymax, ymin,
<THEME_FUNCTION> x, y, lower, middle, upper, ymax, ymin, alpha, alpha, color, group, linetype, size, width
color, fill, group, linetype, shape, size, weight Also geom_errorbarh().
ggplot(data = mpg, aes(x = cty, y = hwy)) Begins a plot ONE VARIABLE continuous
that you finish by adding layers to. Add one geom c <- ggplot(mpg, aes(hwy)); c2 <- ggplot(mpg) f + geom_dotplot(binaxis = "y", stackdir = “center") j + geom_linerange()
function per layer. x, y, alpha, color, fill, group x, ymin, ymax, alpha, color, group, linetype, size
c + geom_area(stat = "bin")
last_plot() Returns the last plot. x, y, alpha, color, fill, linetype, size f + geom_violin(scale = “area") j + geom_pointrange() - x, y, ymin, ymax,
x, y, alpha, color, fill, group, linetype, size, weight alpha, color, fill, group, linetype, shape, size
ggsave("plot.png", width = 5, height = 5) Saves last plot c + geom_density(kernel = "gaussian")
as 5’ x 5’ file named "plot.png" in working directory. x, y, alpha, color, fill, group, linetype, size, weight
Matches file type to file extension. both discrete maps
c + geom_dotplot()
g <- ggplot(diamonds, aes(cut, color)) data <- data.frame(murder = USArrests$Murder,
x, y, alpha, color, fill
state = tolower(rownames(USArrests)))
Aes Common aesthetic values. c + geom_freqpoly()
x, y, alpha, color, group, linetype, size
g + geom_count()
x, y, alpha, color, fill, shape, size, stroke
map <- map_data("state")
k <- ggplot(data, aes(fill = murder))
color and fill - string ("red", "#RRGGBB") k + geom_map(aes(map_id = state), map = map)
e + geom_jitter(height = 2, width = 2)
linetype - integer or string (0 = "blank", 1 = "solid", c + geom_histogram(binwidth = 5) x, y, alpha, color, fill, shape, size + expand_limits(x = map$long, y = map$lat)
2 = "dashed", 3 = "dotted", 4 = "dotdash", 5 = "longdash", x, y, alpha, color, fill, linetype, size, weight map_id, alpha, color, fill, linetype, size
6 = "twodash")
c2 + geom_qq(aes(sample = hwy))
lineend - string ("round", "butt", or "square") x, y, alpha, color, fill, linetype, size, weight THREE VARIABLES
linejoin - string ("round", "mitre", or "bevel") seals$z <- with(seals, sqrt(delta_long^2 + delta_lat^2)); l <- ggplot(seals, aes(long, lat))
size - integer (line width in mm) l + geom_contour(aes(z = z)) l + geom_raster(aes(fill = z), hjust = 0.5,
discrete x, y, z, alpha, color, group, linetype, size, weight vjust = 0.5, interpolate = FALSE)
shape - integer/shape name or d <- ggplot(mpg, aes(fl))
a single character ("a") x, y, alpha, fill
d + geom_bar() l + geom_contour_filled(aes(fill = z)) l + geom_tile(aes(fill = z))
x, alpha, color, fill, linetype, size, weight x, y, alpha, color, fill, group, linetype, size, subgroup x, y, alpha, color, fill, linetype, size, width

RCC BY SA Posit So ware, PBC • info@posit.co • posit.co • Learn more at ggplot2.tidyverse.org • ggplot2 3.3.5 • Updated: 2021-08
ft
Stats An alternative way to build a layer. Scales Override defaults with scales package. Coordinate Systems Faceting
A stat builds new variables to plot (e.g., count, prop). Scales map data values to the visual values of an r <- d + geom_bar() Facets divide a plot into
fl cty cyl aesthetic. To change a mapping, add a new scale. r + coord_cartesian(xlim = c(0, 5)) - xlim, ylim subplots based on the
n <- d + geom_bar(aes(fill = fl)) The default cartesian coordinate system. values of one or more

+ =
x ..count..
discrete variables.
aesthetic prepackaged scale-specific r + coord_fixed(ratio = 1/2)
scale_ to adjust scale to use arguments ratio, xlim, ylim - Cartesian coordinates with t <- ggplot(mpg, aes(cty, hwy)) + geom_point()
data stat geom coordinate plot
x=x· system n + scale_fill_manual( fixed aspect ratio between x and y units.
y = ..count.. values = c("skyblue", "royalblue", "blue", "navy"), t + facet_grid(cols = vars(fl))
Visualize a stat by changing the default stat of a geom limits = c("d", "e", "p", "r"), breaks =c("d", "e", "p", “r"), ggplot(mpg, aes(y = fl)) + geom_bar() Facet into columns based on fl.
name = "fuel", labels = c("D", "E", "P", "R")) Flip cartesian coordinates by switching
function, geom_bar(stat="count") or by using a stat
x and y aesthetic mappings. t + facet_grid(rows = vars(year))
function, stat_count(geom="bar"), which calls a default range of title to use in labels to use breaks to use in
values to include legend/axis in legend/axis legend/axis Facet into rows based on year.
geom to make a layer (equivalent to a geom function). in mapping
Use ..name.. syntax to map stat variables to aesthetics. r + coord_polar(theta = "x", direction=1)
theta, start, direction - Polar coordinates. t + facet_grid(rows = vars(year), cols = vars(fl))
GENERAL PURPOSE SCALES Facet into both rows and columns.
geom to use stat function geommappings r + coord_trans(y = “sqrt") - x, y, xlim, ylim t + facet_wrap(vars(fl))
Use with most aesthetics Transformed cartesian coordinates. Set xtrans
i + stat_density_2d(aes(fill = ..level..), Wrap facets into a rectangular layout.
scale_*_continuous() - Map cont’ values to visual ones. and ytrans to the name of a window function.
geom = "polygon")
variable created by stat scale_*_discrete() - Map discrete values to visual ones. Set scales to let axis limits vary across facets.
scale_*_binned() - Map continuous values to discrete bins. π + coord_quickmap()
60
π + coord_map(projection = "ortho", orientation t + facet_grid(rows = vars(drv), cols = vars(fl),
c + stat_bin(binwidth = 1, boundary = 10) scale_*_identity() - Use data values as visual ones. = c(41, -74, 0)) - projection, xlim, ylim scales = "free")

lat
x, y | ..count.., ..ncount.., ..density.., ..ndensity.. scale_*_manual(values = c()) - Map discrete values to Map projections from the mapproj package x and y axis limits adjust to individual facets:
manually chosen visual ones.
c + stat_count(width = 1) x, y | ..count.., ..prop.. long
(mercator (default), azequalarea, lagrange, etc.). "free_x" - x axis limits adjust
scale_*_date(date_labels = "%m/%d"), "free_y" - y axis limits adjust
c + stat_density(adjust = 1, kernel = "gaussian") date_breaks = "2 weeks") - Treat data values as dates.
x, y | ..count.., ..density.., ..scaled..
e + stat_bin_2d(bins = 30, drop = T)
scale_*_datetime() - Treat data values as date times.
Same as scale_*_date(). See ?strptime for label formats.
Position Adjustments Set labeller to adjust facet label:
t + facet_grid(cols = vars(fl), labeller = label_both)
x, y, fill | ..count.., ..density.. Position adjustments determine how to arrange geoms fl: c fl: d fl: e fl: p fl: r
X & Y LOCATION SCALES that would otherwise occupy the same space.
e + stat_bin_hex(bins = 30) x, y, fill | ..count.., ..density.. t + facet_grid(rows = vars(fl),
Use with x or y aesthetics (x shown here) s <- ggplot(mpg, aes(fl, fill = drv)) labeller = label_bquote(alpha ^ .(fl)))
e + stat_density_2d(contour = TRUE, n = 100)
x, y, color, size | ..level.. scale_x_log10() - Plot x on log10 scale. ↵c ↵d ↵e ↵p ↵r
scale_x_reverse() - Reverse the direction of the x axis. s + geom_bar(position = "dodge")
e + stat_ellipse(level = 0.95, segments = 51, type = "t") scale_x_sqrt() - Plot x on square root scale. Arrange elements side by side.
l + stat_contour(aes(z = z)) x, y, z, order | ..level..
l + stat_summary_hex(aes(z = z), bins = 30, fun = max) COLOR AND FILL SCALES (DISCRETE)
s + geom_bar(position = "fill")
Stack elements on top of one
Labels and Legends
x, y, z, fill | ..value.. another, normalize height. Use labs() to label the elements of your plot.
n + scale_fill_brewer(palette = "Blues")
l + stat_summary_2d(aes(z = z), bins = 30, fun = mean) For palette choices: e + geom_point(position = "jitter") t + labs(x = "New x axis label", y = "New y axis label",
x, y, z, fill | ..value.. RColorBrewer::display.brewer.all() Add random noise to X and Y position of title ="Add a title above the plot",
each element to avoid overplotting. subtitle = "Add a subtitle below title",
f + stat_boxplot(coef = 1.5) n + scale_fill_grey(start = 0.2, A caption = "Add a caption below plot",
x, y | ..lower.., ..middle.., ..upper.., ..width.. , ..ymin.., ..ymax.. end = 0.8, na.value = "red") e + geom_label(position = "nudge") alt = "Add alt text to the plot",
B
Nudge labels away from points. <aes> = "New <aes>
<AES> <AES> legend title")
f + stat_ydensity(kernel = "gaussian", scale = "area") x, y
| ..density.., ..scaled.., ..count.., ..n.., ..violinwidth.., ..width.. COLOR AND FILL SCALES (CONTINUOUS) s + geom_bar(position = "stack") t + annotate(geom = "text", x = 8, y = 9, label = “A")
Stack elements on top of one another. Places a geom with manually selected aesthetics.
e + stat_ecdf(n = 40) x, y | ..x.., ..y.. o <- c + geom_dotplot(aes(fill = ..x..))
e + stat_quantile(quantiles = c(0.1, 0.9), Each position adjustment can be recast as a function p + guides(x = guide_axis(n.dodge = 2)) Avoid crowded
o + scale_fill_distiller(palette = “Blues”) with manual width and height arguments: or overlapping labels with guide_axis(n.dodge or angle).
formula = y ~ log(x), method = "rq") x, y | ..quantile..
s + geom_bar(position = position_dodge(width = 1)) n + guides(fill = “none") Set legend type for each
e + stat_smooth(method = "lm", formula = y ~ x, se = T, o + scale_fill_gradient(low="red", high=“yellow") aesthetic: colorbar, legend, or none (no legend).
level = 0.95) x, y | ..se.., ..x.., ..y.., ..ymin.., ..ymax..
ggplot() + xlim(-5, 5) + stat_function(fun = dnorm,
o + scale_fill_gradient2(low = "red", high = “blue”,
mid = "white", midpoint = 25) Themes n + theme(legend.position = "bottom")
Place legend at "bottom", "top", "le ", or “right”.
n = 20, geom = “point”) x | ..x.., ..y.. n + scale_fill_discrete(name = "Title",
ggplot() + stat_qq(aes(sample = 1:100)) o + scale_fill_gradientn(colors = topo.colors(6)) r + theme_bw() r + theme_classic() labels = c("A", "B", "C", "D", "E"))
x, y, sample | ..sample.., ..theoretical.. Also: rainbow(), heat.colors(), terrain.colors(), White background Set legend title and labels with a scale function.
cm.colors(), RColorBrewer::brewer.pal() with grid lines. r + theme_light()
e + stat_sum() x, y, size | ..n.., ..prop..
e + stat_summary(fun.data = "mean_cl_boot")
h + stat_summary_bin(fun = "mean", geom = "bar")
SHAPE AND SIZE SCALES
r + theme_gray()
Grey background
r + theme_linedraw()
r + theme_minimal()
Zooming
p <- e + geom_point(aes(shape = fl, size = cyl)) (default theme). Minimal theme. Without clipping (preferred):
e + stat_identity() p + scale_shape() + scale_size() r + theme_dark() r + theme_void() t + coord_cartesian(xlim = c(0, 100), ylim = c(10, 20))
e + stat_unique() p + scale_shape_manual(values = c(3:7)) Dark for contrast. Empty theme.
With clipping (removes unseen data points):
r + theme() Customize aspects of the theme such
as axis, legend, panel, and facet properties. t + xlim(0, 100) + ylim(10, 20)
p + scale_radius(range = c(1,6))
p + scale_size_area(max_size = 6) r + ggtitle(“Title”) + theme(plot.title.postion = “plot”) t + scale_x_continuous(limits = c(0, 100)) +
r + theme(panel.background = element_rect(fill = “blue”)) scale_y_continuous(limits = c(0, 100))

CC BY SA Posit So ware, PBC • info@posit.co • posit.co • Learn more at ggplot2.tidyverse.org • ggplot2 3.3.5 • Updated: 2021-08
ft
ft
Data tidying with tidyr : : CHEAT SHEET
Tidy data is a way to organize tabular data in a
consistent data structure across packages. Reshape Data - Pivot data to reorganize values into a new layout. Expand
A table is tidy if:
A B C A B C
table4a Tables
country 1999 2000 country year cases pivot_longer(data, cols, names_to = "name", Create new combinations of variables or identify
& A
B
0.7K 2K
37K 80K
A
B
1999 0.7K
1999 37K
values_to = "value", values_drop_na = FALSE) implicit missing values (combinations of
C 212K 213K C 1999 212K "Lengthen" data by collapsing several columns variables not present in the data).
A 2000 2K
Each variable is in Each observation, or into two. Column names move to a new
B 2000 80K x
its own column case, is in its own row C 2000 213K names_to column and values to a new values_to x1 x2 x3 x1 x2 expand(data, …) Create a
column. A 1 3
B 1 4
A 1
A 2 new tibble with all possible
A B C A *B C pivot_longer(table4a, cols = 2:3, names_to ="year", B 2 3 B 1
B 2
combinations of the values
values_to = "cases") of the variables listed in …
Drop other variables.
table2 expand(mtcars, cyl, gear,
Access variables Preserve cases in country year type count country year cases pop pivot_wider(data, names_from = "name", carb)
as vectors vectorized operations A 1999 cases 0.7K A 1999 0.7K 19M
values_from = "value")
A 1999 pop 19M A 2000 2K 20M x
A 2000 cases 2K B 1999 37K 172M The inverse of pivot_longer(). "Widen" data by x1 x2 x3 x1 x2 x3 complete(data, …, fill =
Tibbles
A 1 3 A 1 3
A
B
2000
1999
pop 20M
cases 37K
B
C
2000 80K 174M
1999 212K 1T
expanding two columns into several. One column B 1 4 A 2 NA list()) Add missing possible
B 1999 pop 172M C 2000 213K 1T provides the new column names, the other the B 2 3 B 1 4
combinations of values of
AN ENHANCED DATA FRAME
B 2 3
B 2000 cases 80K values. variables listed in … Fill
Tibbles are a table format provided B 2000 pop 174M
remaining variables with NA.
C 1999 cases 212K pivot_wider(table2, names_from = type,
by the tibble package. They inherit the complete(mtcars, cyl, gear,
C 1999 pop 1T values_from = count)
data frame class, but have improved behaviors: C 2000 cases 213K carb)
C 2000 pop 1T
• Subset a new tibble with ], a vector with [[ and $.
• No partial matching when subsetting columns.
• Display concise views of the data on one screen. Split Cells - Use these functions to split or combine cells into individual, isolated values. Handle Missing Values
options(tibble.print_max = n, tibble.print_min = m, table5 Drop or replace explicit missing values (NA).
tibble.width = Inf) Control default display settings. country century year country year unite(data, col, …, sep = "_", remove = TRUE,
x
View() or glimpse() View the entire data set.
A 19 99 A 1999
na.rm = FALSE) Collapse cells across several x1 x2 x1 x2 drop_na(data, …) Drop
A 20 00 A 2000
B 19 99 B 1999 columns into a single column. A 1 A 1
rows containing NA’s in …
CONSTRUCT A TIBBLE B NA D 3
B 20 00 B 2000
unite(table5, century, year, col = "year", sep = "") C
D
NA
3
columns.
tibble(…) Construct by columns. E NA drop_na(x, x2)
tibble(x = 1:3, y = c("a", "b", "c")) Both make table3
x
this tibble country year rate country year cases pop separate(data, col, into, sep = "[^[:alnum:]]+",
tribble(…) Construct by rows. x1 x2 x1 x2 fill(data, …, .direction =
A 1999 0.7K/19M0 A 1999 0.7K 19M remove = TRUE, convert = FALSE, extra = "warn", A 1 A 1
tribble(~x, ~y, A 2000 0.2K/20M0 A 2000 2K 20M B NA B 1 "down") Fill in NA’s in …
A tibble: 3 × 2 fill = "warn", …) Separate each cell in a column
1, "a", x y B 1999 .37K/172M B 1999 37K 172 C NA C 1
columns using the next or
<int> <chr> B 2000 .80K/174M B 2000 80K 174 into several columns. Also extract(). D 3 D 3
2, "b", 1 1 a
E NA E 3 previous value.
3, "c") 2
3
2
3
b
c
separate(table3, rate, sep = "/", fill(x, x2)
into = c("cases", "pop"))
x
as_tibble(x, …) Convert a data frame to a tibble. table3
country
A
year
1999
rate
0.7K x1 x2 x1 x2 replace_na(data, replace)
A 1 A 1
enframe(x, name = "name", value = "value") country year rate A 1999 19M
Specify a value to replace
A 1999 0.7K/19M0 A 2000 2K separate_rows(data, …, sep = "[^[:alnum:].]+", B NA B 2
Convert a named vector to a tibble. Also deframe(). C NA C 2
NA in selected columns.
A 2000 0.2K/20M0 A 2000 20M
convert = FALSE) Separate each cell in a column D 3 D 3

is_tibble(x) Test whether x is a tibble. B 1999 .37K/172M B 1999 37K


into several rows.
E NA E 2 replace_na(x, list(x2 = 2))
B 2000 .80K/174M B 1999 172M
B 2000 80K
B 2000 174M separate_rows(table3, rate, sep = "/")

CC BY SA Posit So ware, PBC • info@posit.co • posit.co • Learn more at tidyr.tidyverse.org • tibble 3.2.1 • tidyr 1.3.0 • Updated: 2023–05
ft
Nested Data
A nested data frame stores individual tables as a list-column of data frames within a larger organizing data frame. List-columns can also be lists of vectors or lists of varying data types.
Use a nested data frame to:
• Preserve relationships between observations and subsets of data. Preserve the type of the variables being nested (factors and datetimes aren't coerced to character).
• Manipulate many sub-tables at once with purrr functions like map(), map2(), or pmap() or with dplyr rowwise() grouping.

CREATE NESTED DATA RESHAPE NESTED DATA TRANSFORM NESTED DATA


nest(data, …) Moves groups of cells into a list-column of a data unnest(data, cols, ..., keep_empty = FALSE) Flatten nested columns A vectorized function takes a vector, transforms each element in
frame. Use alone or with dplyr::group_by(): back to regular columns. The inverse of nest(). parallel, and returns a vector of the same length. By themselves
n_storms |> unnest(data) vectorized functions cannot work with lists, such as list-columns.
1. Group the data frame with group_by() and use nest() to move
the groups into a list-column. unnest_longer(data, col, values_to = NULL, indices_to = NULL) dplyr::rowwise(.data, …) Group data so that each row is one
n_storms <- storms |> Turn each element of a list-column into a row. group, and within the groups, elements of list-columns appear
group_by(name) |> directly (accessed with [[ ), not as lists of length one. When you
nest() starwars |> use rowwise(), dplyr functions will seem to apply functions to
select(name, films) |> list-columns in a vectorized fashion.
2. Use nest(new_col = c(x, y)) to specify the columns to group
using dplyr::select() syntax. unnest_longer(films)
n_storms <- storms |>
name films
nest(data = c(year:long)) data data data result
Luke The Empire Strik…
<tibble [50x4]> <tibble [50x4]> fun( <tibble [50x4]> , …) result 1
"cell" contents Luke Revenge of the S… <tibble [50x4]> <tibble [50x4]> fun( <tibble [50x4]> , …) result 2
yr lat long name films Luke Return of the Jed… <tibble [50x4]> <tibble [50x4]> fun( <tibble [50x4]> , …) result 3
name yr lat long name yr lat long 1975 27.5 -79.0 Luke <chr [5]> C-3PO The Empire Strik…
Amy 1975 27.5 -79.0 Amy 1975 27.5 -79.0 1975 28.5 -79.0 C-3PO <chr [6]> C-3PO Attack of the Cl…
Amy Amy 1975 28.5 -79.0 nested data frame 1975 29.5 -79.0
1975 28.5 -79.0 R2-D2 <chr[7]> C-3PO The Phantom M…
Amy 1975 29.5 -79.0 Amy 1975 29.5 -79.0
Bob 1979 22.0 -96.0 Bob 1979 22.0 -96.0
name data
Amy <tibble [50x3]>
yr lat long
1979 22.0 -96.0
R2-D2 The Empire Strik… Apply a function to a list-column and create a new list-column.
Bob 1979 22.5 -95.3
R2-D2 Attack of the Cl…
Bob 1979 22.5 -95.3 Bob <tibble [50x3]> 1979 22.5 -95.3
Bob 1979 23.0 -94.6 R2-D2 The Phantom M…
Bob 1979 23.0 -94.6 Zeta <tibble [50x3]> 1979 23.0 -94.6 dim() returns two
Zeta 2005 23.9 -35.6 Zeta 2005 23.9 -35.6
yr lat long
n_storms |> values per row
Zeta 2005 24.2 -36.1 Zeta
Zeta
2005
2005
24.2
24.7
-36.1
-36.6
2005 23.9 -35.6 rowwise() |>
Zeta 2005 24.7 -36.6
2005 24.2 -36.1 unnest_wider(data, col) Turn each element of a list-column into a mutate(n = list(dim(data))) wrap with list to tell mutate
to create a list-column
2005 24.7 -36.6
Index list-columns with [[]]. n_storms$data[[1]] regular column.
starwars |>
select(name, films) |> Apply a function to a list-column and create a regular column.
CREATE TIBBLES WITH LIST-COLUMNS unnest_wider(films, names_sep = “_")
tibble::tribble(…) Makes list-columns when needed.
tribble( ~max, ~seq, n_storms |>
3, 1:3, max seq name films name films_1 films_2 films_3 rowwise() |>
Luke <chr [5]> Luke The Empire... Revenge of... Return of... nrow() returns one
4, 1:4,
3
4
<int [3]>
<int [4]>
mutate(n = nrow(data)) integer per row
C-3PO <chr [6]> C-3PO The Empire... Attack of... The Phantom...
5, 1:5) 5 <int [5]>
R2-D2 <chr[7]> R2-D2 The Empire... Attack of... The Phantom...

tibble::tibble(…) Saves list input as list-columns.


tibble(max = c(3, 4, 5), seq = list(1:3, 1:4, 1:5)) Collapse multiple list-columns into a single list-column.
tibble::enframe(x, name="name", value="value") hoist(.data, .col, ..., .remove = TRUE) Selectively pull list components
Converts multi-level list to a tibble with list-cols. out into their own top-level columns. Uses purrr::pluck() syntax for starwars |> append() returns a list for each
row, so col type must be list
enframe(list('3'=1:3, '4'=1:4, '5'=1:5), 'max', 'seq') selecting from lists. rowwise() |>
mutate(transport = list(append(vehicles, starships)))
OUTPUT LIST-COLUMNS FROM OTHER FUNCTIONS starwars |>
dplyr::mutate(), transmute(), and summarise() will output select(name, films) |>
Apply a function to multiple list-columns.
list-columns if they return a list. hoist(films, first_film = 1, second_film = 2)
mtcars |> starwars |> length() returns one
integer per row
group_by(cyl) |> name films name first_film second_film films rowwise() |>
summarise(q = list(quantile(mpg))) Luke <chr [5]> Luke The Empire… Revenge of… <chr [3]> mutate(n_transports = length(c(vehicles, starships)))
C-3PO <chr [6]> C-3PO The Empire… Attack of… <chr [4]>
R2-D2 <chr[7]> R2-D2 The Empire… Attack of… <chr [5]>
See purrr package for more list functions.

CC BY SA Posit So ware, PBC • info@posit.co • posit.co • Learn more at tidyr.tidyverse.org • tibble 3.2.1 • tidyr 1.3.0 • Updated: 2023–05
ft
String manipulation with stringr : : CHEAT SHEET
The stringr package provides a set of internally consistent tools for working with character strings, i.e. sequences of characters surrounded by quotation marks.

Detect Matches Subset Strings Manage Lengths


TRUE str_detect(string, pattern, negate = FALSE) str_sub(string, start = 1L, end = -1L) Extract 4 str_length(string) The width of strings (i.e.
TRUE
FALSE
Detect the presence of a pattern match in a substrings from a character vector. 6
2
number of code points, which generally equals
TRUE string. Also str_like(). str_detect(fruit, "a") str_sub(fruit, 1, 3); str_sub(fruit, -2) 3 the number of characters). str_length(fruit)
TRUE str_starts(string, pattern, negate = FALSE) str_subset(string, pattern, negate = FALSE) str_pad(string, width, side = c("le ", "right",
TRUE
FALSE
Detect the presence of a pattern match at Return only the strings that contain a pattern "both"), pad = " ") Pad strings to constant
TRUE the beginning of a string. Also str_ends(). match. str_subset(fruit, "p") width. str_pad(fruit, 17)
str_starts(fruit, "a")
str_extract(string, pattern) Return the first str_trunc(string, width, side = c("right", "le ",
1 str_which(string, pattern, negate = FALSE) NA
pattern match found in each string, as a vector. "center"), ellipsis = "...") Truncate the width
2
4
Find the indexes of strings that contain Also str_extract_all() to return every pattern of strings, replacing content with ellipsis.
a pattern match. str_which(fruit, "a") match. str_extract(fruit, "[aeiou]") str_trunc(sentences, 6)
start end

2 4 str_locate(string, pattern) Locate the str_match(string, pattern) Return the str_trim(string, side = c("both", "le ", "right"))
4 7
NA NA
positions of pattern matches in a string. NA NA
first pattern match found in each string, as Trim whitespace from the start and/or end of
3 4 Also str_locate_all(). str_locate(fruit, "a") a matrix with a column for each ( ) group in a string. str_trim(str_pad(fruit, 17))
pattern. Also str_match_all().
0 str_count(string, pattern) Count the number str_match(sentences, "(a|the) ([^ +])") str_squish(string) Trim whitespace from each
3
1
of matches in a string. str_count(fruit, "a") end and collapse multiple spaces into single
2 spaces. str_squish(str_pad(fruit, 17, "both"))

Mutate Strings Join and Split Order Strings


str_sub() <- value. Replace substrings by str_c(..., sep = "", collapse = NULL) Join 4 str_order(x, decreasing = FALSE, na_last =
identifying the substrings with str_sub() and multiple strings into a single string. 1
3
TRUE, locale = "en", numeric = FALSE, ...)1
assigning into the results. str_c(letters, LETTERS) 2 Return the vector of indexes that sorts a
str_sub(fruit, 1, 3) <- "str" character vector. fruit[str_order(fruit)]
str_flatten(string, collapse = "") Combines
str_replace(string, pattern, replacement) into a single string, separated by collapse. str_sort(x, decreasing = FALSE, na_last =
Replace the first matched pattern in each str_flatten(fruit, ", ") TRUE, locale = "en", numeric = FALSE, ...)1
string. Also str_remove(). Sort a character vector. str_sort(fruit)
str_replace(fruit, "p", "-") str_dup(string, times) Repeat strings times
times. Also str_unique() to remove duplicates.
str_replace_all(string, pattern, replacement)
Replace all matched patterns in each string.
str_dup(fruit, times = 2)
Helpers
Also str_remove_all(). str_split_fixed(string, pattern, n) Split a str_conv(string, encoding) Override the
str_replace_all(fruit, "p", "-") vector of strings into a matrix of substrings encoding of a string. str_conv(fruit,"ISO-8859-1")
(splitting at occurrences of a pattern match).
A STRING str_to_lower(string, locale = "en")1 Also str_split() to return a list of substrings appl<e> str_view_all(string, pattern, match = NA)
a string Convert strings to lower case. and str_split_n() to return the nth substring. banana View HTML rendering of all regex matches.
str_to_lower(sentences) str_split_fixed(sentences, " ", n=3) p<e>ar Also str_view() to see only the first match.
str_view_all(sentences, "[aeiou]")
a string str_to_upper(string, locale = "en")1 {xx} {yy} str_glue(…, .sep = "", .envir = parent.frame())
A STRING Convert strings to upper case. Create a string from strings and {expressions} TRUE
str_equal(x, y, locale = "en", ignore_case =
str_to_upper(sentences) to evaluate. str_glue("Pi is {pi}") TRUE
FALSE FALSE, ...)1 Determine if two strings are
TRUE equivalent. str_equal(c("a", "b"), c("a", "c"))
a string str_to_title(string, locale = "en")1 Convert str_glue_data(.x, ..., .sep = "", .envir =
A String strings to title case. Also str_to_sentence(). parent.frame(), .na = "NA") Use a data frame, This is a long sentence. str_wrap(string, width = 80, indent = 0,
str_to_title(sentences) list, or environment to create a string from exdent = 0) Wrap strings into nicely formatted
This is a long
strings and {expressions} to evaluate. sentence. paragraphs. str_wrap(sentences, 20)
str_glue_data(mtcars, "{rownames(mtcars)} has
{hp} hp") 1 See bit.ly/ISO639-1 for a complete list of locales.

CC BY SA Posit So ware, PBC • info@posit.co • posit.co • Learn more at stringr.tidyverse.org • Diagrams from @LVaudor on Twitter • stringr 1.5.0 • Updated: 2023-05
ft
ft
ft
ft
Need to Know Regular Expressions - Regular expressions, or regexps, are a concise language for
describing patterns in strings.
[:space:]
new line
Pattern arguments in stringr are interpreted as MATCH CHARACTERS see <- function(rx) str_view_all("abc ABC 123\t.!?\\(){}\n", rx)
regular expressions a er any special characters [:blank:] .
have been parsed. string regexp matches example
(type this) (to mean this) (which matches this) space
In R, you write regular expressions as strings, a (etc.) a (etc.) see("a") abc ABC 123 .!?\(){} tab
sequences of characters surrounded by quotes \\. \. . see("\\.") abc ABC 123 .!?\(){}
("") or single quotes('').
\\! \! ! see("\\!") abc ABC 123 .!?\(){} [:graph:]
Some characters cannot be represented directly \\? \? ? see("\\?") abc ABC 123 .!?\(){}
in an R string . These must be represented as \\\\ \\ \ see("\\\\") abc ABC 123 .!?\(){} [:punct:] [:symbol:]
special characters, sequences of characters that \\( \( ( see("\\(") abc ABC 123 .!?\(){}
have a specific meaning., e.g. . , : ; ? ! / *@# | ` = + ^
\\) \) ) see("\\)") abc ABC 123 .!?\(){}
Special Character Represents \\{ \{ { see("\\{") abc ABC 123 .!?\(){} - _ " ' [ ] { } ( ) ~ < > $
\\ \ \\} \} } see( "\\}") abc ABC 123 .!?\(){}
\" " \\n \n new line (return) see("\\n") abc ABC 123 .!?\(){} [:alnum:]
\n new line \\t \t tab see("\\t") abc ABC 123 .!?\(){}
Run ?"'" to see a complete list \\s \s any whitespace (\S for non-whitespaces) see("\\s") abc ABC 123 .!?\(){} [:digit:]
\\d \d any digit (\D for non-digits) see("\\d") abc ABC 123 .!?\(){}
0 1 2 3 4 5 6 7 8 9
Because of this, whenever a \ appears in a regular \\w \w any word character (\W for non-word chars) see("\\w") abc ABC 123 .!?\(){}
expression, you must write it as \\ in the string \\b \b word boundaries see("\\b") abc ABC 123 .!?\(){}
that represents the regular expression. [:digit:]
1
digits see("[:digit:]") abc ABC 123 .!?\(){} [:alpha:]
1
Use writeLines() to see how R views your string [:alpha:] letters see("[:alpha:]") abc ABC 123 .!?\(){} [:lower:] [:upper:]
1
a er all special characters have been parsed. [:lower:] lowercase letters see("[:lower:]") abc ABC 123 .!?\(){}
[:upper:]
1
uppercase letters see("[:upper:]") abc ABC 123 .!?\(){} a b c d e f A B C D E F
writeLines("\\.") 1
# \. [:alnum:] letters and numbers see("[:alnum:]") abc ABC 123 .!?\(){} g h i j k l GH I J K L
[:punct:] 1 punctuation see("[:punct:]") abc ABC 123 .!?\(){}
mn o p q r MNOPQR
writeLines("\\ is a backslash") [:graph:]1 letters, numbers, and punctuation see("[:graph:]") abc ABC 123 .!?\(){}
# \ is a backslash
[:space:]1 space characters (i.e. \s) see("[:space:]") abc ABC 123 .!?\(){} s t u v w x S T U VWX
[:blank:]1 space and tab (but not new line) see("[:blank:]") abc ABC 123 .!?\(){} y z Y Z
. every character except a new line see(".") abc ABC 123 .!?\(){}
INTERPRETATION 1 Many base R functions require classes to be wrapped in a second set of [ ], e.g. [[:digit:]]
Patterns in stringr are interpreted as regexs. To
change this default, wrap the pattern in one of:
ALTERNATES alt <- function(rx) str_view_all("abcde", rx) QUANTIFIERS quant <- function(rx) str_view_all(".a.aa.aaa", rx)
regex(pattern, ignore_case = FALSE, multiline = example example
regexp matches regexp matches
FALSE, comments = FALSE, dotall = FALSE, ...)
Modifies a regex to ignore cases, match end of ab|d or alt("ab|d") abcde a? zero or one quant("a?") .a.aa.aaa
lines as well of end of strings, allow R comments [abe] one of alt("[abe]") abcde a* zero or more quant("a*") .a.aa.aaa
within regex's , and/or to have . match everything a+ one or more quant("a+") .a.aa.aaa
including \n. [^abe] anything but alt("[^abe]") abcde
str_detect("I", regex("i", TRUE)) [a-c] range alt("[a-c]") abcde 1 2 ... n a{n} exactly n quant("a{2}") .a.aa.aaa
1 2 ... n a{n, } n or more quant("a{2,}") .a.aa.aaa
fixed() Matches raw bytes but will miss some n ... m a{n, m} between n and m quant("a{2,4}") .a.aa.aaa
characters that can be represented in multiple ANCHORS anchor <- function(rx) str_view_all("aaa", rx)
ways (fast). str_detect("\u0130", fixed("i")) regexp matches example
^a start of string anchor("^a") aaa GROUPS ref <- function(rx) str_view_all("abbaab", rx)
coll() Matches raw bytes and will use locale
specific collation rules to recognize characters a$ end of string anchor("a$") aaa Use parentheses to set precedent (order of evaluation) and create groups
that can be represented in multiple ways (slow).
regexp matches example
str_detect("\u0130", coll("i", TRUE, locale = "tr"))
(ab|d)e sets precedence alt("(ab|d)e") abcde
LOOK AROUNDS look <- function(rx) str_view_all("bacad", rx)
boundary() Matches boundaries between
characters, line_breaks, sentences, or words. regexp matches example Use an escaped number to refer to and duplicate parentheses groups that occur
str_split(sentences, boundary("word")) a(?=c) followed by look("a(?=c)") bacad earlier in a pattern. Refer to each group by its order of appearance
a(?!c) not followed by look("a(?!c)") bacad string regexp matches example
(?<=b)a preceded by look("(?<=b)a") bacad (type this) (to mean this) (which matches this) (the result is the same as ref("abba"))

(?<!b)a not preceded by look("(?<!b)a") bacad \\1 \1 (etc.) first () group, etc. ref("(a)(b)\\2\\1") abbaab

CC BY SA Posit So ware, PBC • info@posit.co • posit.co • Learn more at stringr.tidyverse.org • Diagrams from @LVaudor on Twitter • stringr 1.5.0 • Updated: 2023-05
ft
ft
ft
Dates and times with lubridate : : CHEAT SHEET
Date-times 2017-11-28 12:00:00 2017-11-28 12:00:00 Round Date-times
A date-time is a point on the timeline, A date is a day stored as An hms is a time stored as floor_date(x, unit = "second")
stored as the number of seconds since the number of days since the number of seconds since Round down to nearest unit.
1970-01-01 00:00:00 UTC 1970-01-01 00:00:00 floor_date(dt, unit = "month")
2016 2017 2018 2019 2020
Jan Feb Mar Apr
dt <- as_datetime(1511870400) d <- as_date(17498) t <- hms::as_hms(85) round_date(x, unit = "second")
2017-11-28 12:00:00 Round to nearest unit.
## "2017-11-28 12:00:00 UTC" ## "2017-11-28" ## 00:01:25
round_date(dt, unit = "month")
Jan Feb Mar Apr ceiling_date(x, unit = "second",
PARSE DATE-TIMES (Convert strings or numbers to date-times) GET AND SET COMPONENTS change_on_boundary = NULL)
d ## "2017-11-28" Round up to nearest unit.
1. Identify the order of the year (y), month (m), day (d), hour (h), Use an accessor function to get a component. day(d) ## 28 ceiling_date(dt, unit = "month")
minute (m) and second (s) elements in your data. Assign into an accessor function to change a day(d) <- 1 Jan Feb Mar Apr

2. Use the function below whose name replicates the order. Each component in place. d ## "2017-11-01" Valid units are second, minute, hour, day, week, month, bimonth,
accepts a tz argument to set the time zone, e.g. ymd(x, tz = "UTC"). quarter, season, halfyear and year.

ymd_hms(), ymd_hm(), ymd_h(). 2018-01-31 11:59:59 date(x) Date component. date(dt) rollback(dates, roll_to_first = FALSE, preserve_hms = TRUE) Roll back to
2017-11-28T14:02:00 ymd_hms("2017-11-28T14:02:00") last day of previous month. Also rollforward(). rollback(dt)
year(x) Year. year(dt)
2017-22-12 10:00:00
ydm_hms(), ydm_hm(), ydm_h().
ydm_hms("2017-22-12 10:00:00")
2018-01-31 11:59:59 isoyear(x) The ISO 8601 year.
epiyear(x) Epidemiological year. Stamp Date-times
mdy_hms(), mdy_hm(), mdy_h(). stamp() Derive a template from an example string and return a new
11/28/2017 1:02:03 2018-01-31 11:59:59 month(x, label, abbr) Month. function that will apply the template to date-times. Also
mdy_hms("11/28/2017 1:02:03") month(dt) stamp_date() and stamp_time().
dmy_hms(), dmy_hm(), dmy_h(). day(x) Day of month. day(dt) 1. Derive a template, create a function
1 Jan 2017 23:59:59 dmy_hms("1 Jan 2017 23:59:59") Tip: use a
2018-01-31 11:59:59 wday(x, label, abbr) Day of week. sf <- stamp("Created Sunday, Jan 17, 1999 3:34") date with
ymd(), ydm(). ymd(20170131) qday(x) Day of quarter. day > 12
20170131 2. Apply the template to dates
sf(ymd("2010-04-05"))
mdy(), myd(). mdy("July 4th, 2000") 2018-01-31 11:59:59 hour(x) Hour. hour(dt) ## [1] "Created Monday, Apr 05, 2010 00:00"
July 4th, 2000
dmy(), dym(). dmy("4th of July '99") 2018-01-31 11:59:59 minute(x) Minutes. minute(dt)
4th of July '99
2001: Q3 yq() Q for quarter. yq("2001: Q3") 2018-01-31 11:59:59 second(x) Seconds. second(dt) Time Zones
R recognizes ~600 time zones. Each encodes the time zone, Daylight
07-2020 my(), ym(). my("07-2020") 2018-01-31 11:59:59 UTC tz(x) Time zone. tz(dt) Savings Time, and historical calendar variations for an area. R assigns
one time zone per vector.
2:01 hms::hms() Also lubridate::hms(), week(x) Week of the year. week(dt)
hm() and ms(), which return x
J F M A M J isoweek() ISO 8601 week. Use the UTC time zone to avoid Daylight Savings.
periods.* hms::hms(seconds = 0, J A S O N D epiweek() Epidemiological week.
minutes = 1, hours = 2) OlsonNames() Returns a list of valid time zone names. OlsonNames()
x
J F M A M J quarter(x) Quarter. quarter(dt) Sys.timezone() Gets current time zone.
J A S O N D
2017.5 date_decimal(decimal, tz = "UTC") 5:00 6:00
semester(x, with_year = FALSE)
date_decimal(2017.5)
x
J F M A M J Semester. semester(dt) 4:00 Mountain Central 7:00 with_tz(time, tzone = "") Get
the same date-time in a new
now(tzone = "") Current time in tz J A S O N D Pacific Eastern time zone (a new clock time).
(defaults to system tz). now() am(x) Is it in the am? am(dt) Also local_time(dt, tz, units).
pm(x) Is it in the pm? pm(dt) with_tz(dt, "US/Pacific")
today(tzone = "") Current date in a PT
MT
January

CT ET
xxxxx dst(x) Is it daylight savings? dst(d)
xxx tz (defaults to system tz). today()
force_tz(time, tzone = "") Get
fast_strptime() Faster strptime. leap_year(x) Is it a leap year? the same clock time in a new
leap_year(d) 7:00 7:00
fast_strptime(“9/1/01”, “%y/%m/%d”) Pacific Eastern time zone (a new date-time).
Also force_tzs().
parse_date_time() Easier strptime. update(object, ..., simple = FALSE) 7:00 7:00 force_tz(dt, "US/Pacific")
parse_date_time(“09-01-01”, "ymd") update(dt, mday = 2, hour = 1) Mountain Central

CC BY SA Posit So ware, PBC • info@posit.co • posit.co • Learn more at lubridate.tidyverse.org • lubridate 1.9.2 • Updated: 2023-05
ft
Math with Date-times — Lubridate provides three classes of timespans to facilitate math with dates and date-times.
Math with date-times relies on the timeline, Periods track changes in clock times, Durations track the passage of Intervals represent specific intervals Not all years
which behaves inconsistently. Consider how which ignore time line irregularities. physical time, which deviates from of the timeline, bounded by start and are 365 days
the timeline behaves during: clock time when irregularities occur. end date-times. due to leap days.
A normal day nor + minutes(90) nor + dminutes(90) interval(nor, nor + minutes(90)) Not all minutes
nor <- ymd_hms("2018-01-01 01:30:00",tz="US/Eastern") are 60 seconds due to
leap seconds.

1:00 2:00 3:00 4:00


It is possible to create an imaginary date
1:00 2:00 3:00 4:00 1:00 2:00 3:00 4:00 1:00 2:00 3:00 4:00
by adding months, e.g. February 31st
The start of daylight savings (spring forward) gap + minutes(90) gap + dminutes(90) interval(gap, gap + minutes(90)) jan31 <- ymd(20180131)
gap <- ymd_hms("2018-03-11 01:30:00",tz="US/Eastern") jan31 + months(1)
## NA
1:00 2:00 3:00 4:00 1:00 2:00 3:00 4:00 1:00 2:00 3:00 4:00 1:00 2:00 3:00 4:00
%m+% and %m-% will roll imaginary
dates to the last day of the previous
The end of daylight savings (fall back) month.
lap + minutes(90) lap + dminutes(90) interval(lap, lap + minutes(90))
lap <- ymd_hms("2018-11-04 00:30:00",tz="US/Eastern")
jan31 %m+% months(1)
## "2018-02-28"
add_with_rollback(e1, e2, roll_to_first =
12:00 1:00 2:00 3:00 12:00 1:00 2:00 3:00 12:00 1:00 2:00 3:00 12:00 1:00 2:00 3:00 TRUE) will roll imaginary dates to the
leap + years(1) leap + dyears(1) interval(leap, leap + years(1)) first day of the new month.
Leap years and leap seconds
leap <- ymd("2019-03-01") add_with_rollback(jan31, months(1),
roll_to_first = TRUE)
## "2018-03-01"
2019 2020 2021 2019 2020 2021 2019 2020 2021 2019 2020 2021

PERIODS DURATIONS INTERVALS


Add or subtract periods to model events that happen at specific clock Add or subtract durations to model physical processes, like battery life. Divide an interval by a duration to determine its physical length, divide
times, like the NYSE opening bell. Durations are stored as seconds, the only time unit with a consistent length. an interval by a period to determine its implied length in clock time.
Di imes are a class of durations found in base R.
Start End
Make a period with the name of a time unit pluralized, e.g. Make a duration with the name of a period prefixed with a d, e.g. Make an interval with interval() or %--%, e.g. Date Date
p <- months(3) + days(12) years(x = 1) x years. dd <- ddays(14) dyears(x = 1) 31536000x seconds. i <- interval(ymd("2017-01-01"), d) ## 2017-01-01 UTC--2017-11-28 UTC
p months(x) x months. dd dmonths(x = 1) 2629800x seconds. j <- d %--% ymd("2017-12-31") ## 2017-11-28 UTC--2017-12-31 UTC
"3m 12d 0H 0M 0S" weeks(x = 1) x weeks. "1209600s (~2 weeks)" dweeks(x = 1) 604800x seconds. a %within% b Does interval or date-time a fall
days(x = 1) x days. Exact Equivalent ddays(x = 1) 86400x seconds. within interval b? now() %within% i
Number Number
etc. hours(x = 1) x hours. length in in common dhours(x = 1) 3600x seconds.
of months of days
minutes(x = 1) x minutes. seconds units dminutes(x = 1) 60x seconds. int_start(int) Access/set the start date-time of
seconds(x = 1) x seconds. dseconds(x = 1) x seconds. an interval. Also int_end(). int_start(i) <- now();
milliseconds(x = 1) x milliseconds. int_start(i)
dmilliseconds(x = 1) x x 10-3 seconds.
microseconds(x = 1) x microseconds dmicroseconds(x = 1) x x 10-6 seconds. int_aligns(int1, int2) Do two intervals share a
nanoseconds(x = 1) x nanoseconds. dnanoseconds(x = 1) x x 10-9 seconds. boundary? Also int_overlaps(). int_aligns(i, j)
picoseconds(x = 1) x picoseconds. dpicoseconds(x = 1) x x 10-12 seconds.
int_di (times) Make the intervals that occur
period(num = NULL, units = "second", ...) duration(num = NULL, units = "second", …) between the date-times in a vector.
An automation friendly period constructor. An automation friendly duration v <-c(dt, dt + 100, dt + 1000); int_di (v)
period(5, unit = "years") constructor. duration(5, unit = "years")
int_flip(int) Reverse the direction of an
as.period(x, unit) Coerce a timespan to a as.duration(x, …) Coerce a timespan to a interval. Also int_standardize(). int_flip(i)
period, optionally in the specified units. duration. Also is.duration(), is.di ime().
Also is.period(). as.period(p) as.duration(i) l int_length(int) Length in seconds. int_length(i)
period_to_seconds(x) Convert a period to make_di ime(x) Make di ime with the
the "standard" number of seconds implied int_shi (int, by) Shi s an interval up or down
specified number of units. the timeline by a timespan. int_shi (i, days(-1))
by the period. Also seconds_to_period(). make_di ime(99999)
period_to_seconds(p)
as.interval(x, start, …) Coerce a timespan to
an interval with the start date-time. Also
is.interval(). as.interval(days(1), start = now())
CC BY SA Posit So ware, PBC • info@posit.co • posit.co • Learn more at lubridate.tidyverse.org • lubridate 1.9.2 • Updated: 2023-05
fft
ff
ft
fft
fft
ft
ft
fft
ff
fft
ft
RStudio IDE : : CHEAT SHEET
Documents and Apps Source Editor Tab Panes Version
Open Shiny, R Markdown,
knitr, Sweave, LaTeX, .Rd files
Navigate

forwards
Open in new Save Find and
backwards/ window replace
Compile as Run
notebook selected
code
Import data History of past
with wizard commands to
run/copy
Manage
external
View
memory
databases usage
R tutorials
Control
and more in Source Pane Turn on at Tools > Project Options > Git/SVN
A• Added M• Modified
Check Render Choose Configure Insert D• Deleted R• Renamed
?• Untracked

spelling output output render code Publish


format options chunk to server
Stage Commit Push/Pull View Current
Re-run Source with or Show file files: staged files to remote History branch
previous code w/out Echo or outline Load Save Clear R Search inside
as a Local Job workspace workspace workspace environment
Jump to Jump Run Show file Visual Multiple cursors/column selection Choose environment to display from Display objects
previous to next code outline Editor with Alt + mouse drag. list of parent environments as list or grid Open shell to type commands
chunk chunk (reverse
side) Code diagnostics that appear in the margin. Show file di to view file di erences
Run this and Hover over diagnostic symbols for details.
Jump to all previous Run this Syntax highlighting based
section code chunks code chunk on your file's extension
or chunk Tab completion to finish function
Set knitr Displays saved objects by View in data View function
chunk names, file paths, arguments, and more. type with short description viewer source code
options

Access markdown guide at


Help > Markdown Quick Reference
See reverse side for more on Visual Editor
Multi-language code snippets to
quickly use common blocks of code. More file
options
Debug Mode
Jump to function in file Change file type Use debug(), browser(), or a breakpoint and execute
Create Delete Rename Change
folder file file directory your code to open the debugger mode.
RStudio recognizes that files named app.R,
server.R, ui.R, and global.R belong to a shiny app Path to displayed directory Launch debugger Open traceback to examine
mode from origin the functions that R called
Working Run scripts in Maximize, of error before the error occurred
Directory separate sessions minimize panes
A File browser keyed to your working directory.
Run Choose Publish to Manage Ctrl/Cmd + arrow- Click on file or directory name to open.
app location to shinyapps.io publish R Markdown Drag pane
up
view app or server accounts to see history Build Log boundaries

Package Development
Click next to line number to Highlighted line shows where
RStudio opens plots in a dedicated Plots pane RStudio opens documentation in a dedicated Help pane add/remove a breakpoint. execution has paused
Create a new package with
File > New Project > New Directory > R Package
Navigate Open in Export Delete Delete
Enable roxygen documentation with recent plots window plot plot all plots Home page of Search within Search for
Tools > Project Options > Build Tools helpful links help file help file
Roxygen guide at Help > Roxygen Quick Reference
See package information in the Build Tab Viewer pane displays HTML content, such as Shiny
apps, RMarkdown reports, and interactive visualizations
GUI Package manager lists every installed package
Install package Run devtools::load_all()
and restart R and reload changes
Stop Shiny Publish to shinyapps.io, Refresh
Install Update Browse app rpubs, RSConnect, … Run commands in Examine variables Select function
Packages Packages package site environment where in executing in traceback to
Clear output execution has paused environment debug
Run R CMD and rebuild
check View(<data>) opens spreadsheet like view of data set
Customize Run Click to load package with Package Delete
package build package library(). Unclick to detach version from
options tests package with detach(). installed library

Filter rows by value Sort by Search Step through Step into and Resume Quit debug
or value range values for value code one line out of functions execution mode
at a time to run
CC BY SA Posit So ware, PBC • info@posit.co • posit.co • Learn more at rstudio.com • Font Awesome 5.15.3 • RStudio IDE 1.4.1717 • Updated: 2021-07
ff
ft
ff
Keyboard Shortcuts RStudio
RUN CODE
Search command history
Windows/Linux
Ctrl+arrow-up
Mac
Cmd+arrow-up
DOCUMENTS AND APPS
Knit Document (knitr) Ctrl+Shi +K Cmd+Shi +K
Workbench
Interrupt current command Esc Esc Insert chunk (Sweave & Knitr) Ctrl+Alt+I Cmd+Option+I WHY RSTUDIO WORKBENCH?
Clear console Ctrl+L Ctrl+L Run from start to current line Ctrl+Alt+B Cmd+Option+B Extend the open source server with a
commercial license, support, and more:
NAVIGATE CODE MORE KEYBOARD SHORTCUTS
Go to File/Function Ctrl+. Ctrl+. Keyboard Shortcuts Help Alt+Shi +K Option+Shi +K • open and run multiple R sessions at once
Show Command Palette Ctrl+Shi +P Cmd+Shi +P • tune your resources to improve performance
WRITE CODE
Attempt completion Tab or Tab or
• administrative tools for managing user sessions
Ctrl+Space Ctrl+Space View the Keyboard Shortcut Quick Search for keyboard shortcuts with • collaborate real-time with others in shared projects
Insert <- (assignment operator) Alt+- Option+- Reference with Tools > Keyboard Tools > Show Command Palette • switch easily from one version of R to a di erent version
Shortcuts or Alt/Option + Shi + K or Ctrl/Cmd + Shi + P.
Insert %>% (pipe operator) Ctrl+Shi +M Cmd+Shi +M • integrate with your authentication, authorization, and audit practices
(Un)Comment selection Ctrl+Shi +C Cmd+Shi +C • work in the RStudio IDE, JupyterLab, Jupyter Notebooks, or VS Code
MAKE PACKAGES Windows/Linux Mac Download a free 45 day evaluation at
Load All (devtools) Ctrl+Shi +L Cmd+Shi +L www.rstudio.com/products/workbench/evaluation/
Test Package (Desktop)
Document Package
Ctrl+Shi +T
Ctrl+Shi +D
Cmd+Shi +T
Cmd+Shi +D Share Projects
File > New Project
RStudio saves the call history,

Visual Editor
workspace, and working Start new R Session Close R Session
Choose Choose Insert Jump to Jump Run directory associated with a in current project in project
Check Render output output code previous to next selected Publish Show file project. It reloads each when
spelling output format location chunk chunk chunk lines to server outline you re-open a project.
T H J
Back to
Source Editor
Block (front page) Active shared
format collaborators
Name of
current
Lists and Links Citations Images File outline project
Insert blocks, Select
block
citations, Insert and Share Project R Version
quotes More
formatting equations, and edit tables with Collaborators
Clear special
formatting characters
Insert
verbatim
code
Run Remote Jobs
Run R on remote clusters
(Kubernetes/Slurm) via the
Job Launcher
Add/Edit
attributes Monitor Launch a job
launcher jobs

Run this and


Set knitr all previous
chunk code chunks
options
Run this
Jump to chunk code chunk
or header

Run launcher
jobs remotely

CC BY SA Posit So ware, PBC • info@posit.co • posit.co • Learn more at rstudio.com • Font Awesome 5.15.3 • RStudio IDE 1.4.1717 • Updated: 2021-07
ft
ft
ft
ft
ft
ft
ft
ft
ft
ft
ft
ft
ft
ft
ft
ft
ft
ft
ft
ff
Factors with forcats : : CHEAT SHEET
The forcats package provides tools for working with factors, which are R's data structure for categorical data.

Factors stored displayed Change the order of levels Change the value of levels
R represents categorical integer
1 1= a a 1= a
data with factors. A factor vector 3 23 == bc c 23 == bc a 1= a a 1= b fct_relevel(.f, ..., a er = 0L) a 1= a v 1= v
2= x
fct_recode(.f, ...) Manually change
is an integer vector with a 2 b c 2= b c 2= c Manually reorder factor levels. c 2= b
z levels. Also fct_relabel() which obeys
3= c 3= a fct_relevel(f, c("b", "c", "a")) 3= c 3= z purrr::map syntax to apply a function
levels attribute that stores levels 1 a b b b x
a set of mappings between or expression to each level.
a a a v fct_recode(f, v = "a", x = "b", z = "c")
integers and categorical values. When you view a factor, R fct_infreq(f, ordered = NA) Reorder
displays not the integers, but the levels associated with them. fct_relabel(f, ~ paste0("x", .x))
levels by the frequency
c 1= a c 1= c in which they appear in the
= c 2= c c 2= a data (highest frequency first).
a a 1= a Also fct_inseq(). a 1= a 2 1=2 fct_anon(f, prefix = "")
c c 2= b a a c 2= b 2=1 Anonymize levels with random
3= c
f3 <- factor(c("c", "c", "a")) 3= c 1 3=3
integers.
b b fct_infreq(f3) b 3 fct_anon(f)
a a a 2
b 1= a b 1= b fct_inorder(f, ordered = NA)
a 2= b a 2= a Reorder levels by order in which
they appear in the data. a 1= a x 1= x fct_collapse(.f, …, other_level = NULL)
a 1= a a
c 2= b c 2= c Collapse levels into manually defined
c 2= b b fct_inorder(f2) 3= c groups.
3= c c b x fct_collapse(f, x = c("a", "b"))
b a x
a a 1= a a 1= c fct_rev(f) Reverse level order.
2= b 2= b f4 <- factor(c("a","b","c"))
b 3= c b 3= a
c c fct_rev(f4) fct_lump_min(f, min, w = NULL,
Inspect Factors a
c
1= a
2= b
3= c
a
Other
1= a
2 = Other
other_level = "Other") Lumps together
factors that appear fewer than min
times. Also fct_lump_n(),
a 1= a f n fct_count(f, sort = FALSE, a 1= a a 1= c fct_shi (f) Shi levels to le or b Other
fct_lump_prop(), and
c 2= b
a 2 prop = FALSE) Count the 2= b 2= a right, wrapping around end. a a
3= c number of values with each b 3= c b 3= b
fct_lump_lowfreq().
b b 1 c c fct_shi (f4) fct_lump_min(f, min = 2)
level. fct_count(f)
a c 1
fct_match(f, lvls) Check for
lvls in f. fct_match(f, "a") a 1= a a 1= a fct_shu le(f, n = 1L) Randomly a 1= a a 1= a fct_other(f, keep, drop, other_level =
2= b 2= c permute order of factor levels. 2= b 2= b "Other") Replace levels with "other."
a 1= a a 1= a fct_unique(f) Return the b 3= c b 3= b
c 3= c
Other
3 = Other
c c fct_shu le(f4) fct_other(f, keep = c("a", "b"))
b 2= b
b 2= b unique values, removing b b
a duplicates. fct_unique(f) a a

Combine Factors Add or drop levels


1= a 1= b fct_reorder(.f, .x, .fun = median,
a 2= b
ca 2= c ..., .desc = FALSE) Reorder levels by their
bc 3= c b 3= a relationship with another variable.
boxplot(
a 1= a + b 1= a = a 1= a fct_c(…) Combine factors PlantGrowth, a 1= a a 1= a fct_drop(f, only) Drop unused levels.
c 2= c a 2= b c 2= c with di erent levels. weight ~ fct_reorder(group, weight)
b 2= b
b 2= b f5 <- factor(c("a","b"),c("a","b","x"))
3= b Also fct_cross(). ) 3= x f6 <- fct_drop(f5)
b f1 <- factor(c("a", "c"))
a f2 <- factor(c("b", "a")) fct_reorder2(.f, .x, .y, .fun = last2, ..., .desc =
fct_c(f1, f2)
1= a
2= b
1= b
2= c TRUE) Reorder levels by their final values a 1= a a 1= a fct_expand(f, …) Add levels to a factor.
2= b 2= b fct_expand(f6, "x")
3= c 3= a when plotted with two other variables.
ggplot(
b b 3= x
a 1= a
2= b
a 1= a
2= b
fct_unify(fs, levels = diamonds,
b b 3= c lvls_union(fs)) Standardize
levels across a list of factors.
aes(
carat, price,
a 1= a a 1= a fct_na_value_to_level(f, level =
a 1= a
2= c
a 1= a
2= b color = fct_reorder2(color, carat, price) b 2= b
b 2= b "(Missing)") Assigns a level to NAs to
c 2c 3= c fct_unify(list(f2, f1)) 3= x ensure they appear in plots, etc.
)) +
geom_smooth()
NA x f7 <- factor(c("a", "b", NA))
fct_na_value_to_level(f7, level = "(Missing)")

CC BY SA Posit So ware, PBC • info@posit.co • posit.co • Learn more at forcats.tidyverse.org • Diagrams inspired by @LVaudor on Twitter • forcats 1.0.0 • Updated: 2023-05
ft
ff
ff
ft
ff
ft
ft
ft
ft
Data import with the tidyverse : : CHEAT SHEET
Read Tabular Data with readr
read_*(file, col_names = TRUE, col_types = NULL, col_select = NULL, id = NULL, locale, n_max = Inf, One of the first steps of a project is to import OTHER TYPES OF DATA
skip = 0, na = c("", "NA"), guess_max = min(1000, n_max), show_col_types = TRUE) See ?read_delim outside data into R. Data is o en stored in Try one of the following
tabular formats, like csv files or spreadsheets. packages to import other types of files:
A|B|C
A B C read_delim("file.txt", delim = "|") Read files with any delimiter. If no The front page of this sheet shows • haven - SPSS, Stata, and SAS files
1 2 3 delimiter is specified, it will automatically guess. how to import and save text files into • DBI - databases
1|2|3 4 5 NA To make file.txt, run: write_file("A|B|C\n1|2|3\n4|5|NA", file = "file.txt")
4|5|NA R using readr. • jsonlite - json
The back page shows how to import • xml2 - XML
A B C read_csv("file.csv") Read a comma delimited file with period • httr - Web APIs
A,B,C spreadsheet data from Excel files
1 2 3 decimal marks. • rvest - HTML (Web Scraping)
1,2,3 4 5 NA write_file("A,B,C\n1,2,3\n4,5,NA", file = "file.csv") using readxl or Google Sheets using
4,5,NA googlesheets4. • readr::read_lines() - text data

read_csv2("file2.csv") Read semicolon delimited files with comma


Column Specification with readr
A B C
A;B;C
1.5 2 3 decimal marks.
1,5;2;3 4.5 5 NA write_file("A;B;C\n1,5;2;3\n4,5;5;NA", file = "file2.csv")
4,5;5;NA Column specifications define what data type each
USEFUL COLUMN ARGUMENTS
column of a file will be imported as. By default
A B C read_tsv("file.tsv") Read a tab delimited file. Also read_table(). readr will generate a column spec when a file is Hide col spec message
A B C read_*(file, show_col_types = FALSE)
1 2 3 read_fwf("file.tsv", fwf_widths(c(2, 2, NA))) Read a fixed width file. read and output a summary.
1 2 3 4 5 NA write_file("A\tB\tC\n1\t2\t3\n4\t5\tNA\n", file = "file.tsv")
4 5 NA spec(x) Extract the full column specification for Select columns to import
the given imported data frame. Use names, position, or selection helpers.
read_*(file, col_select = c(age, earn))
USEFUL READ ARGUMENTS spec(x)
# cols(
A B C No header 1 2 3 Skip lines # age = col_integer(), age is an
integer Guess column types
1 2 3 read_csv("file.csv", col_names = FALSE) 4 5 NA read_csv("file.csv", skip = 1) # edu = col_character(),
4 5 NA # earn = col_double() To guess a column type, read_ *() looks at the
A B C Read a subset of lines # ) first 1000 rows of data. Increase with guess_max.
x y z Provide header 1 2 3 read_csv("file.csv", n_max = 1) edu is a read_*(file, guess_max = Inf)
A B C read_csv("file.csv", earn is a double (numeric) character
1 2 3 col_names = c("x", "y", "z")) A B C Read values as missing
4 5 NA
NA 2 3 read_csv("file.csv", na = c("1")) COLUMN TYPES DEFINE COLUMN SPECIFICATION
4 5 NA
Read multiple files into a single table Each column type has a function and Set a default type
read_csv(c(“f1.csv”, “f2.csv”, “f3.csv"), Specify decimal marks corresponding string abbreviation. read_csv(
A;B;C
id = "origin_file") read_delim("file2.csv", locale = file,
1,5;2;3,0 locale(decimal_mark = ",")) • col_logical() - "l"
col_type = list(.default = col_double())
• col_integer() - "i" )
• col_double() - "d"
Save Data with readr • col_number() - "n"
Use column type or string abbreviation
read_csv(
• col_character() - "c" file,
write_*(x, file, na = "NA", append, col_names, quote, escape, eol, num_threads, progress) • col_factor(levels, ordered = FALSE) - "f" col_type = list(x = col_double(), y = "l", z = "_")
• col_datetime(format = "") - "T" )
A B C write_delim(x, file, delim = " ") Write files with any delimiter. • col_date(format = "") - "D" Use a single string of abbreviations
A,B,C • col_time(format = "") - "t"
1 2 3
write_csv(x, file) Write a comma delimited file. # col types: skip, guess, integer, logical, character
4 5 NA 1,2,3 • col_skip() - "-", "_" read_csv(
4,5,NA write_csv2(x, file) Write a semicolon delimited file. • col_guess() - "?" file,
col_type = "_?ilc"
write_tsv(x, file) Write a tab delimited file. )

CC BY SA Posit So ware, PBC • info@posit.co • posit.co • readr.tidyverse.org • readxl.tidyverse.org and googlesheets4.tidyverse.org • readr 2.1.4 • readxl 1.4.2 • googlesheets4 1.1.0 • Updated: 2023-05
ft
ft
Import Spreadsheets
with readxl with googlesheets4
READ EXCEL FILES READ SHEETS
A B C D E A B C D E
1 x1 x2 x3 x4 x5 x1 x2 x3 x4 x5 1 x1 x2 x3 x4 x5 x1 x2 x3 x4 x5
2 x z 8 x NA z 8 NA 2 x z 8 x NA z 8 NA
3 y 7 9 10 y 7 NA 9 10 READXL COLUMN SPECIFICATION 3 y 7 9 10 y 7 NA 9 10 GOOGLESHEETS4 COLUMN SPECIFICATION
s1 s1
Column specifications define what data type Column specifications define what data type
each column of a file will be imported as. each column of a file will be imported as.
read_excel(path, sheet = NULL, range = NULL) read_sheet(ss, sheet = NULL, range = NULL)
Read a .xls or .xlsx file based on the file extension. Read a sheet from a URL, a Sheet ID, or a dribble
Use the col_types argument of read_excel() to Use the col_types argument of read_sheet()/
See front page for more read arguments. Also from the googledrive package. See front page for
set the column specification. range_read() to set the column specification.
read_xls() and read_xlsx(). more read arguments. Same as range_read().
read_excel("excel_file.xlsx")
Guess column types Guess column types
To guess a column type, read_ excel() looks at SHEETS METADATA To guess a column type read_sheet()/
READ SHEETS the first 1000 rows of data. Increase with the URLs are in the form: range_read() looks at the first 1000 rows of data.
guess_max argument. https://docs.google.com/spreadsheets/d/ Increase with guess_max.
A B C D E read_excel(path, sheet = read_excel(path, guess_max = Inf) read_sheet(path, guess_max = Inf)
NULL) Specify which sheet SPREADSHEET_ID/edit#gid=SHEET_ID
to read by position or name. Set all columns to same type, e.g. character gs4_get(ss) Get spreadsheet meta data. Set all columns to same type, e.g. character
read_excel(path, sheet = 1) read_excel(path, col_types = "text") read_sheet(path, col_types = "c")
s1 s2 s3
read_excel(path, sheet = "s1") gs4_find(...) Get data on all spreadsheet files.
Set each column individually sheet_properties(ss) Get a tibble of properties Set each column individually
read_excel( for each worksheet. Also sheet_names(). # col types: skip, guess, integer, logical, character
excel_sheets(path) Get a
vector of sheet names. path, read_sheets(ss, col_types = "_?ilc")
s1 s2 s3
col_types = c("text", "guess", "guess",“numeric") WRITE SHEETS
excel_sheets("excel_file.xlsx")
) A B C write_sheet(data, ss =
1 x 4 1 1 x 4 NULL, sheet = NULL) COLUMN TYPES
A B C D E To read multiple sheets: 2 y 5 2 2 y 5
Write a data frame into a
COLUMN TYPES l n c D L
A B C D E 1. Get a vector of sheet 3 z 6 3 3 z 6
new or existing Sheet. TRUE 2 hello 1947-01-08 hello
s1
names from the file path. logical numeric text date list FALSE 3.45 world 1956-10-21 1
A B C D E gs4_create(name, ...,
2. Set the vector names to TRUE 2 hello 1947-01-08 hello
s1 s2 A B C D sheets = NULL) Create a
be the sheet names. FALSE 3.45 world 1956-10-21 1 • skip - "_" or "-" • date - "D"
1 new Sheet with a vector
s1 s2 3. Use purrr::map_dfr() to • guess - "?" • datetime - "T"
• skip • logical • date 2 of names, a data frame,
s1 s2 s3 read multiple files into • logical - "l" • character - "c"
• guess • numeric • list s1 or a (named) list of data
one data frame. • integer - "i" • list-column - "L"
• text frames.
• double - "d" • cell - "C" Returns
path <- "your_file_path.xlsx" A B C
sheet_append(ss, data,
x1 x2 x3 1 x1 x2 x3 • numeric - "n" list of raw cell data.
path |> excel_sheets() |> Use list for columns that include multiple data 2 1 x 4 sheet = 1) Add rows to
2 y 5
set_names() |> types. See tidyr and purrr for list-column data. 3 z 6 3 2 y 5 the end of a worksheet. Use list for columns that include multiple data
map_dfr(read_excel, path = path) 4 3 z 6 types. See tidyr and purrr for list-column data.
s1

OTHER USEFUL EXCEL PACKAGES CELL SPECIFICATION FOR READXL AND GOOGLESHEETS4 FILE LEVEL OPERATIONS
For functions to write data to Excel files, see: Use the range argument of readxl::read_excel() or googlesheets4 also o ers ways to modify other
• openxlsx googlesheets4::read_sheet() to read a subset of cells from a aspects of Sheets (e.g. freeze rows, set column
• writexl A B C D E sheet. width, manage (work)sheets). Go to
1 1 2 3 4 5 2 3 4 read_excel(path, range = "Sheet1!B1:D2") googlesheets4.tidyverse.org to read more.
For working with non-tabular Excel data, see: 2 x y z NA y z read_sheet(ss, range = "B1:D2")
• tidyxl 3 6 7 9 10 For whole-file operations (e.g. renaming, sharing,
s1 Also use the range argument with cell specification functions placing within a folder), see the tidyverse
cell_limits(), cell_rows(), cell_cols(), and anchored(). package googledrive at
googledrive.tidyverse.org.

CC BY SA Posit So ware, PBC • info@posit.co • posit.co • readr.tidyverse.org • readxl.tidyverse.org and googlesheets4.tidyverse.org • readr 2.1.4 • readxl 1.4.2 • googlesheets4 1.1.0 • Updated: 2023-05
ft
ff
Data transformation with dplyr : : CHEAT SHEET
dplyr functions work with pipes and expect tidy data. In tidy data:
A B C A B C
Manipulate Cases Manipulate Variables
&
pipes EXTRACT CASES EXTRACT VARIABLES
Row functions return a subset of rows as a new table. Column functions return a set of columns as a new vector or table.
Each variable is in Each observation, or x |> f(y)
its own column case, is in its own row becomes f(x, y) filter(.data, …, .preserve = FALSE) Extract rows pull(.data, var = -1, name = NULL, …) Extract

Summarize Cases w
www
ww that meet logical criteria.
mtcars |> filter(mpg > 20) w
www column values as a vector, by name or index.
mtcars |> pull(wt)

distinct(.data, …, .keep_all = FALSE) Remove select(.data, …) Extract columns as a table.

w
www
Apply summary functions to columns to create a new table of

w
www
ww
rows with duplicate values. mtcars |> select(mpg, wt)
summary statistics. Summary functions take vectors as input and mtcars |> distinct(gear)
return one value (see back).
relocate(.data, …, .before = NULL, .a er = NULL)
slice(.data, …, .preserve = FALSE) Select rows

w
www
ww
summary function Move columns to new position.
by position. mtcars |> relocate(mpg, cyl, .a er = last_col())
mtcars |> slice(10:15)
summarize(.data, …)

w
ww w
www
ww
Compute table of summaries. slice_sample(.data, …, n, prop, weight_by =
mtcars |> summarize(avg = mean(mpg)) NULL, replace = FALSE) Randomly select rows. Use these helpers with select() and across()
Use n to select a number of rows and prop to e.g. mtcars |> select(mpg:cyl)
count(.data, …, wt = NULL, sort = FALSE, name = select a fraction of rows.
NULL) Count number of rows in each group defined contains(match) num_range(prefix, range) :, e.g., mpg:cyl
mtcars |> slice_sample(n = 5, replace = TRUE) ends_with(match) all_of(x)/any_of(x, …, vars) !, e.g., !gear
by the variables in … Also tally(), add_count(),

w
ww add_tally(). starts_with(match) matches(match) everything()
mtcars |> count(cyl) slice_min(.data, order_by, …, n, prop,
with_ties = TRUE) and slice_max() Select rows
with the lowest and highest values. MANIPULATE MULTIPLE VARIABLES AT ONCE
Group Cases w
www
ww
mtcars |> slice_min(mpg, prop = 0.25)
df <- tibble(x_1 = c(1, 2), x_2 = c(3, 4), y = c(4, 5))
slice_head(.data, …, n, prop) and slice_tail()
Use group_by(.data, …, .add = FALSE, .drop = TRUE) to create a Select the first or last rows. across(.cols, .funs, …, .names = NULL) Summarize

w
ww
"grouped" copy of a table grouped by columns in ... dplyr mtcars |> slice_head(n = 5) or mutate multiple columns in the same way.
functions will manipulate each "group" separately and combine df |> summarize(across(everything(), mean))
the results.
Logical and boolean operators to use with filter() c_across(.cols) Compute across columns in

w
ww
== < <= is.na() %in% | xor() row-wise data.

w
www
ww mtcars |> != > >= !is.na() ! &
df |>
rowwise() |>
w
group_by(cyl) |>
summarize(avg = mean(mpg)) See ?base::Logic and ?Comparison for help. mutate(x_total = sum(c_across(1:2)))
MAKE NEW VARIABLES
ARRANGE CASES Apply vectorized functions to columns. Vectorized functions take
Use rowwise(.data, …) to group data into individual rows. dplyr arrange(.data, …, .by_group = FALSE) Order vectors as input and return vectors of the same length as output
functions will compute results for each row. Also apply functions (see back).
w
www
ww
rows by values of a column or columns (low to
to list-columns. See tidyr cheat sheet for list-column workflow. high), use with desc() to order from high to low. vectorized function
mtcars |> arrange(mpg) mutate(.data, …, .keep = "all", .before = NULL,
starwars |> mtcars |> arrange(desc(mpg))

ww
www w
www
ww
.a er = NULL) Compute new column(s). Also

w
w ww
rowwise() |> add_column().
mutate(film_count = length(films)) mtcars |> mutate(gpm = 1 / mpg)
ADD CASES mtcars |> mutate(gpm = 1 / mpg, .keep = "none")
add_row(.data, …, .before = NULL, .a er = NULL)
ungroup(x, …) Returns ungrouped copy of table.

w
www
ww
Add one or more rows to a table. rename(.data, …) Rename columns. Use

w
www
w
g_mtcars <- mtcars |> group_by(cyl) cars |> add_row(speed = 1, dist = 1) rename_with() to rename with a function.
ungroup(g_mtcars) mtcars |> rename(miles_per_gallon = mpg)

CC BY SA Posit So ware, PBC • info@posit.co • posit.co • Learn more at dplyr.tidyverse.org • dplyr 1.1.2 • Updated: 2023-05
ft
ft
ft
ft
ft
Vectorized Functions Summary Functions Combine Tables
TO USE WITH MUTATE () TO USE WITH SUMMARIZE () COMBINE VARIABLES COMBINE CASES
mutate() applies vectorized functions to summarize() applies summary functions to x y
columns to create new columns. Vectorized columns to create a new table. Summary A B C E F G A B C E F G A B C

functions take vectors as input and return


vectors of the same length as output.
functions take vectors as input and return single
values as output.
a
b
c
t
u
v
1
2
3
+ a
b
d
t
u
w
3
2
1
= a
b
c
t
u
v
1
2
3
a
b
d
t
u
w
3
2
1
x
a t 1
b u 2
A B C

vectorized function summary function


bind_cols(…, .name_repair) Returns tables
placed side by side as a single table. Column
+ y
c v 3
d w 4 bind_rows(…, .id = NULL)
Returns tables one on top of the
lengths must be equal. Columns will NOT be DF A B C other as a single table. Set .id to
matched by id (to do that look at Relational Data x a t 1
a column name to add a column
OFFSET COUNT below), so be sure to check that both tables are
x
y
b
c
u
v
2
3 of the original table names (as
dplyr::lag() - o set elements by 1 dplyr::n() - number of values/rows ordered the way you want before binding. y d w 4 pictured).
dplyr::lead() - o set elements by -1 dplyr::n_distinct() - # of uniques
sum(!is.na()) - # of non-NAs RELATIONAL DATA
CUMULATIVE AGGREGATE
dplyr::cumall() - cumulative all() POSITION Use a "Mutating Join" to join one table to Use a "Filtering Join" to filter one table against
dplyr::cumany() - cumulative any() columns from another, matching values with the the rows of another.
cummax() - cumulative max() mean() - mean, also mean(!is.na()) rows that they correspond to. Each join retains a
median() - median x y
dplyr::cummean() - cumulative mean() di erent combination of values from the tables. A B C A B D
cummin() - cumulative min()
cumprod() - cumulative prod()
cumsum() - cumulative sum()
LOGICAL
A B C D le _join(x, y, by = NULL, copy = FALSE,
a
b
c
t
u
v
1
2
3
+ a
b
d
t
u
w
3
2
1
=
mean() - proportion of TRUEs
sum() - # of TRUEs
a t 1 3
su ix = c(".x", ".y"), …, keep = FALSE,
RANKING
b u 2 2
na_matches = "na") Join matching
A B C semi_join(x, y, by = NULL, copy = FALSE,
c v 3 NA
values from y to x.
a t 1
…, na_matches = "na") Return rows of x
dplyr::cume_dist() - proportion of all values <= ORDER b u 2
that have a match in y. Use to see what
dplyr::dense_rank() - rank w ties = min, no gaps will be included in a join.
dplyr::min_rank() - rank with ties = min dplyr::first() - first value A B C D right_join(x, y, by = NULL, copy = FALSE,
dplyr::ntile() - bins into n bins dplyr::last() - last value a t 1 3
su ix = c(".x", ".y"), …, keep = FALSE,
dplyr::percent_rank() - min_rank scaled to [0,1] dplyr::nth() - value in nth location of vector b u 2 2
na_matches = "na") Join matching A B C anti_join(x, y, by = NULL, copy = FALSE,
dplyr::row_number() - rank with ties = "first"
d w NA 1
values from x to y.
c v 3
…, na_matches = "na") Return rows of x
RANK that do not have a match in y. Use to see
MATH inner_join(x, y, by = NULL, copy = FALSE, what will not be included in a join.
quantile() - nth quantile A B C D

+, - , *, /, ^, %/%, %% - arithmetic ops min() - minimum value


a t 1 3
su ix = c(".x", ".y"), …, keep = FALSE,
log(), log2(), log10() - logs
b u 2 2
na_matches = "na") Join data. Retain Use a "Nest Join" to inner join one table to
max() - maximum value another into a nested data frame.
<, <=, >, >=, !=, == - logical comparisons only rows with matches.
dplyr::between() - x >= le & x <= right SPREAD A B C y nest_join(x, y, by = NULL, copy =
dplyr::near() - safe == for floating point numbers A B C D full_join(x, y, by = NULL, copy = FALSE, a t 1 <tibble [1x2]>
FALSE, keep = FALSE, name =
IQR() - Inter-Quartile Range a t 1 3
su ix = c(".x", ".y"), …, keep = FALSE, b u 2 <tibble [1x2]>
MISCELLANEOUS mad() - median absolute deviation b u 2 2 c v 3 <tibble [1x2]> NULL, …) Join data, nesting
c v 3 NA na_matches = "na") Join data. Retain all matches from y in a single new
dplyr::case_when() - multi-case if_else() sd() - standard deviation d w NA 1 values, all rows.
var() - variance data frame column.
starwars |>
mutate(type = case_when(
height > 200 | mass > 200 ~ "large",
species == "Droid" ~ "robot", Row Names COLUMN MATCHING FOR JOINS SET OPERATIONS

TRUE ~ "other") Tidy data does not use rownames, which store a A B C intersect(x, y, …)
A B.x C B.y D Use by = c("col1", "col2", …) to
) variable outside of the columns. To work with the
c v 3
Rows that appear in both x and y.
a t 1 t 3
specify one or more common
dplyr::coalesce() - first non-NA values by rownames, first move them into a column. b u 2 u 2
columns to match on.
element across a set of vectors c v 3 NA NA
setdi (x, y, …)
tibble::rownames_to_column() le _join(x, y, by = "A") A B C
dplyr::if_else() - element-wise if() + else() A B C A B
a t 1 Rows that appear in x but not y.
dplyr::na_if() - replace specific values with NA 1 a t 1 a t Move row names into col. b u 2

pmax() - element-wise max() a <- mtcars |>


A.x B.x C A.y B.y Use a named vector, by = c("col1" =
2 b u 2 b u a t 1 d w union(x, y, …)
pmin() - element-wise min() 3 c v 3 c v
rownames_to_column(var = "C") "col2"), to match on columns that A B C
b u 2 b u a t 1 Rows that appear in x or y,
c v 3 a t have di erent names in each table. b u 2
duplicates removed). union_all()
tibble::column_to_rownames() le _join(x, y, by = c("C" = "D")) c v 3
A B C A B d w 4 retains duplicates.
1 a t t 1 a
Move col into row names.
2 b u u 2 b
a |> column_to_rownames(var = "C") A1 B1 C A2 B2 Use su ix to specify the su ix to
3 c v v 3 c a t 1 d w
give to unmatched columns that Use setequal() to test whether two data sets
b u 2 b u
have the same name in both tables. contain the exact same rows (in any order).
Also tibble::has_rownames() and c v 3 a t
tibble::remove_rownames(). le _join(x, y, by = c("C" = "D"),
su ix = c("1", "2"))

CC BY SA Posit So ware, PBC • info@posit.co • posit.co • Learn more at dplyr.tidyverse.org • dplyr 1.1.2 • Updated: 2023-05
ft
ft
ft
ft
ff
ff
ff
ff
ff
ff
ff
ff
ff
ft
ff
ff
ft
ff
Shiny : : CHEAT SHEET
Building an App To generate the template, type shinyapp and press Tab in the RStudio IDE
or go to File > New Project > New Directory > Shiny Web Application
Inputs
Collect values from the user.
A Shiny app is a web page (ui)
# app.R
connected to a computer library(shiny) Access the current value of an input object with
running a live R session (server). Customize the UI with Layout Functions input$<inputId>. Input values are reactive.
In ui nest R ui <- uidPage(
functions to numericInput(inputId = "n",
Add Inputs with *Input() functions actionButton(inputId, label, icon,
build an HTML "Sample size", value = 25), width, …)
interface plotOutput(outputId = "hist") Add Outputs with *Output() functions
) actionLink(inputId, label, icon, …)
Users can manipulate the UI,
which will cause the server to checkboxGroupInput(inputId,
Tell the server server <- function(input, output, session) {
update the UI’s displays (by output$hist <- renderPlot({ Wrap code in render*() functions label, choices, selected, inline, width,
how to render choiceNames, choiceValues)
running R code). hist(rnorm(input$n)) before saving to output
outputs and })
respond to } Refer to UI inputs with input$<id> checkboxInput(inputId, label, value,
Save your template as app.R. inputs with R and outputs with output$<id> width)
Keep your app in a directory
along with optional extra files. shinyApp(ui = ui, server = server) dateInput(inputId, label, value, min,
max, format, startview, weekstart,
app-name Call shinyApp() to combine ui and server into an interactive app! language, width, autoclose,
The directory name is the app name datesdisabled, daysofweekdisabled)
le
.r app.R
(optional) used in showcase mode
le DESCRIPTION
README (optional) directory of supplemental .R files that are sourced dateRangeInput(inputId, label, start,
le
automatically, must be named "R" See annotated examples of Shiny apps by running end, min, max, format, startview,
fo R/ runExample(<example name>). Run runExample() weekstart, language, separator, width,
www/ (optional) directory of files to share with web browsers (images, autoclose)
fo CSS, .js, etc.), must be named "www" with no arguments for a list of example names.
Launch apps stored in a directory with runApp(<path to directory>). fileInput(inputId, label, multiple,
accept, width, buttonLabel, placeholder)

Share Outputs render*() and *Output() functions work together to add R output to the UI. numericInput(inputId, label, value,
min, max, step, width)
Share your app in three ways: DT::renderDataTable(expr, options, dataTableOutput(outputId)
searchDelay, callback, escape, env, quoted, passwordInput(inputId, label, value,
1. Host it on shinyapps.io, a cloud based outputArgs) width, placeholder)
service from RStudio. To deploy Shiny apps:
renderImage(expr, env, quoted, deleteFile, imageOutput(outputId, width, height, radioButtons(inputId, label,
Create a free or professional outputArgs) click, dblclick, hover, brush, inline) choices, selected, inline, width,
account at shinyapps.io choiceNames, choiceValues)
renderPlot(expr, width, height, res, …, alt, env, plotOutput(outputId, width, height, click,
Click the Publish icon in RStudio IDE, or run: quoted, execOnResize, outputArgs) dblclick, hover, brush, inline) selectInput(inputId, label, choices,
rsconnect::deployApp("<path to directory>") selected, multiple, selectize, width, size)
Also selectizeInput()
renderPrint(expr, env, quoted, width, verbatimTextOutput(outputId,
2. Purchase RStudio Connect, a outputArgs) placeholder)
publishing platform for R and Python. sliderInput(inputId, label, min, max,
value, step, round, format, locale, ticks,
rstudio.com/products/connect/ renderTable(expr, striped, hover, bordered, tableOutput(outputId) animate, width, sep, pre, post,
spacing, width, align, rownames, colnames, timeFormat, timezone, dragRange)
3. Build your own Shiny Server digits, na, …, env, quoted, outputArgs)
rstudio.com/products/shiny/shiny-server/ textOutput(outputId, container, inline) submitButton(text, icon, width)
renderText(expr, env, quoted, outputArgs, sep) (Prevent reactions for entire app)
renderUI(expr, env, quoted, outputArgs) uiOutput(outputId, inline, container, …)
htmlOutput(outputId, inline, container, …) textInput(inputId, label, value, width,
placeholder) Also textAreaInput()
These are the core output types. See htmlwidgets.org for many more options.
CC BY SA Posit So ware, PBC • info@posit.co • posit.co • Learn more at shiny.rstudio.com • Font Awesome 5.15.3 • shiny 1.6.0 • Updated: 2021-07
fi
fi
fi
fl
ft
Reactivity UI - An app’s UI is an HTML document. Layouts
Reactive values work together with reactive functions. Call a reactive value from within the arguments of one Use Shiny’s functions to assemble this HTML with R. Combine multiple elements
of these functions to avoid the error Operation not allowed without an active reactive context. uidPage( into a "single element" that
textInput("a","") Returns has its own properties with a
) HTML panel function, e.g.
## <div class="container- uid"> wellPanel(
## <div class="form-group shiny-input-container"> dateInput("a", ""),
## <label for="a"></label> submitButton()
## <input id="a" type="text" )
## class="form-control" value=""/>
## </div> absolutePanel() navlistPanel()
## </div> conditionalPanel() sidebarPanel()
fixedPanel() tabPanel()
headerPanel() tabsetPanel()
Add static HTML elements with tags, a list inputPanel() titlePanel()
of functions that parallel common HTML mainPanel() wellPanel()
tags, e.g. tags$a(). Unnamed arguments
will be passed into the tag; named Organize panels and elements into a layout with a
arguments will become tag attributes. layout function. Add elements as arguments of the
layout functions.
Run names(tags) for a complete list. sidebarLayout()
tags$h1("Header") -> <h1>Header</h1> ui <- fluidPage(
sidebarLayout(
The most common tags have wrapper functions. You side main sidebarPanel(),
do not need to prefix their names with tags$ mainPanel()
CREATE YOUR OWN REACTIVE VALUES RENDER REACTIVE OUTPUT
panel
panel )
*Input() functions render*() functions ui <- uidPage( )
# *Input() example library(shiny)
(see front page) (see front page) h1("Header 1"),
fluidRow()
ui <- uidPage( ui <- uidPage( hr(),
textInput("a","","A") Each input function creates textInput("a","","A"), br(), ui <- fluidPage(
) textOutput("b") Builds an object to row
column col
a reactive value stored as p(strong("bold")), fluidRow(column(width = 4),
) display. Will rerun code in
input$<inputId>. p(em("italic")),
column(width = 2, o set = 3)),
server <- function(input,output){ body to rebuild the object p(code("code")),
output$b <- whenever a reactive value a(href="", "link"), column fluidRow(column(width = 12))
#reactiveValues example
reactiveValues(…) renderText({
in the code changes. HTML("<p>Raw html</p>") )
server <- function(input,output){ input$a
rv <- reactiveValues() Creates a list of reactive }) )
Also flowLayout(), splitLayout(), verticalLayout(),
rv$number <- 5 values whose values you } Save the results to fixedPage(), and fixedRow().
}
can set. shinyApp(ui, server)
output$<outputId>.
To include a CSS file, use includeCSS(), or
1. Place the file in the www subdirectory Layer tabPanels on top of each other,
CREATE REACTIVE EXPRESSIONS PERFORM SIDE EFFECTS and navigate between them, with:
2. Link to it with:
library(shiny) reactive(x, env, quoted, observeEvent(eventExpr, ui <- fluidPage( tabsetPanel(
ui <- uidPage( label, domain)
library(shiny)
handlerExpr, event.env, tags$head(tags$link(rel = "stylesheet", tabPanel("tab 1", "contents"),
ui <- uidPage( event.quoted, handler.env, type = "text/css", href = "< le name>")) tabPanel("tab 2", "contents"),
textInput("a","","A"),
textInput("z","","Z"),
Reactive expressions: textInput("a","","A"),
handler.quoted, ..., label, tabPanel("tab 3", "contents")))
textOutput("b")) • cache their value to )
actionButton("go","Go")
suspended, priority, domain, To include JavaScript, use includeScript() or
server <- function(input,output){
reduce computation autoDestroy, ignoreNULL, 1. Place the file in the www subdirectory ui <- fluidPage( navlistPanel(
• can be called elsewhere server <- function(input,output){
ignoreInit, once) tabPanel("tab 1", "contents"),
re <- reactive({
observeEvent(input$go,{ 2. Link to it with: tabPanel("tab 2", "contents"),
paste(input$a,input$z)}) • notify dependencies print(input$a) Runs code in 2nd
output$b <- renderText({
when invalidated }) tabPanel("tab 3", "contents")))
re()
} argument when reactive tags$head(tags$script(src = "< le name>"))
}
}) Call the expression with values in 1st argument ui <- navbarPage(title = "Page",
function syntax, e.g. re(). change. See observe() for IMAGES To include an image:
shinyApp(ui, server)
shinyApp(ui, server) tabPanel("tab 1", "contents"),
alternative. 1. Place the file in the www subdirectory tabPanel("tab 2", "contents"),
2. Link to it with img(src="< le name>") tabPanel("tab 3", "contents"))
REACT BASED ON EVENT REMOVE REACTIVITY
eventReactive(eventExpr, isolate(expr)
library(shiny)
ui <- uidPage( valueExpr, event.env, library(shiny)

event.quoted, value.env, ui <- uidPage( Runs a code block. Themes


textInput("a","","A"),
textInput("a","","A"), Returns a non-reactive Use the bslib package to add existing themes to Build your own theme by customizing individual
actionButton("go","Go"), value.quoted, ..., label,
textOutput("b")
domain, ignoreNULL, )
textOutput("b") copy of the results. your Shiny app ui, or make your own. arguments.
)
ignoreInit) server <- function(input,output){ bs_theme(bg = "#558AC5",
server <- function(input,output){
re <- eventReactive( Creates reactive output$b <- library(bslib)
fg = "#F9B02D",
renderText({
input$go,{input$a}) expression with code in isolate({input$a})
ui <- uidPage(
...)
theme = bs_theme(
output$b <- renderText({
re()
2nd argument that only })
bootswatch = "darkly",
}) invalidates when reactive }
... ?bs_theme for a full list
} values in 1st argument shinyApp(ui, server) ) of arguments.
change. )
bs_themer() Place within the server function to
bootswatch_themes() Get a list of themes. use the interactive theming widget.
CC BY SA Posit So ware, PBC • info@posit.co • posit.co • Learn more at shiny.rstudio.com • Font Awesome 5.15.3 • shiny 1.6.0 • Updated: 2021-07
fl
fl
fl
fl
fl
fl
fl
fl
fl
ft
fl
ff
fi
fi
fi

You might also like