### Initialize data for recoding examples Source: https://dplyr.tidyverse.org/articles/recoding-replacing Sets up two vectors containing IDs for banned shoes and false starts, which will be used in subsequent recoding operations. ```r id_banned_shoes <- c(2, 10, 15, 32, 65) id_false_start <- c(1, 2, 5, 20, 55, 74, 91) ``` -------------------------------- ### Install dplyr and tidyverse Source: https://dplyr.tidyverse.org/llms.txt Install the entire tidyverse or just the dplyr package using install.packages(). ```r install.packages("tidyverse") # Alternatively, install just dplyr: install.packages("dplyr") ``` -------------------------------- ### Install and Load httpfs Extension Source: https://duckplyr.tidyverse.org/ These commands install and load the httpfs extension for DuckDB, enabling it to query remote files. ```R db_exec("INSTALL httpfs") db_exec("LOAD httpfs") ``` -------------------------------- ### Example of cumall() and cumany() with logical vectors Source: https://dplyr.tidyverse.org/reference/cumall Illustrates how cumall() and cumany() return logical vectors, showing their behavior with conditions. ```r # `cumall()` and `cumany()` return logicals cumall(x < 5) #> [1] TRUE TRUE FALSE FALSE FALSE cumany(x == 3) #> [1] FALSE TRUE TRUE TRUE TRUE ``` -------------------------------- ### Install dbplyr Source: https://dbplyr.tidyverse.org/ Install dbplyr from CRAN or the development version from GitHub. ```r install.packages("tidyverse") install.packages("dbplyr") # Or the development version from GitHub: # install.packages("pak") pak::pak("tidyverse/dbplyr") ``` -------------------------------- ### Install duckplyr from GitHub Source: https://duckplyr.tidyverse.org/ Install the development version of duckplyr directly from its GitHub repository using the 'pak' package manager. ```r # install.packages("pak") pak::pak("tidyverse/duckplyr") ``` -------------------------------- ### Create a Subset Tibble for Mutating Examples in R Source: https://dplyr.tidyverse.org/articles/dplyr Sets up a smaller tibble by selecting specific columns ('name', 'height', 'mass') from the 'starwars' dataset for use in subsequent mutating operation examples. ```r df <- starwars |> select(name, height, mass) ``` -------------------------------- ### Get first row of each group with do() Source: https://dplyr.tidyverse.org/articles/rowwise Demonstrates how to use `do()` to get the first row of each group. This functionality is now superseded by `pick()` and `reframe()`. ```r mtcars |> group_by(cyl) | do(head(., 1)) ``` -------------------------------- ### Install Development Version of dplyr Source: https://dplyr.tidyverse.org/llms.txt Install the development version of dplyr from GitHub using the pak package. ```r # install.packages("pak") pak::pak("tidyverse/dplyr") ``` -------------------------------- ### Example of cummean() with numeric vector Source: https://dplyr.tidyverse.org/reference/cumall Demonstrates the use of cummean() which returns a numeric/integer vector of the same length as the input. It also shows an equivalent calculation using cumsum() and seq_along(). ```r # `cummean()` returns a numeric/integer vector of the same length # as the input vector. x <- c(1, 3, 5, 2, 2) cummean(x) #> [1] 1.00 2.00 3.00 2.75 2.60 cumsum(x) / seq_along(x) #> [1] 1.00 2.00 3.00 2.75 2.60 ``` -------------------------------- ### Get first row of each group with reframe() and pick() Source: https://dplyr.tidyverse.org/articles/rowwise Shows the modern approach to getting the first row of each group using `reframe()` and `pick(everything())`. This replaces the older `do()` method. ```r mtcars | group_by(cyl) | reframe(head(pick(everything()), 1)) ``` -------------------------------- ### Arrange rows by columns selected with `pick()` Source: https://dplyr.tidyverse.org/reference/arrange Shows how to use `arrange()` with `pick()` to select columns based on a pattern, such as those starting with 'Sepal'. This is useful for arranging by multiple related columns. ```R iris |> arrange(pick(starts_with("Sepal"))) ``` -------------------------------- ### Define tibbles for join examples Source: https://dplyr.tidyverse.org/articles/two-table Initializes two tibbles, df1 and df2, which will be used to demonstrate different join operations. ```r df1 <- tibble(x = c(1, 2), y = 2:1) df2 <- tibble(x = c(3, 1), a = 10, b = "a") ``` -------------------------------- ### Compute query and save in remote table Source: https://dplyr.tidyverse.org/reference/compute This example demonstrates how to compute a query and store the results in a remote table using the `compute()` function. It requires setting up a database connection and copying data. ```r mtcars2 <- dbplyr::src_memdb() | copy_to(mtcars, name = "mtcars2-cc", overwrite = TRUE) remote <- mtcars2 | filter(cyl == 8) | select(mpg:drat) # Compute query and save in remote table compute(remote) ``` -------------------------------- ### Basic lag() and lead() usage in R Source: https://dplyr.tidyverse.org/reference/lead-lag Demonstrates the fundamental usage of lag() and lead() to get previous and next values, respectively. NA is used for padding by default. ```r lag(1:5) #> [1] NA 1 2 3 4 lead(1:5) #> [1] 2 3 4 5 NA ``` ```r x <- 1:5 tibble(behind = lag(x), x, ahead = lead(x)) #> # A tibble: 5 × 3 #> behind x ahead #> #> 1 NA 1 2 #> 2 1 2 3 #> 3 2 3 4 #> 4 3 4 5 #> 5 4 5 NA ``` -------------------------------- ### Using if_all() with a predicate Source: https://dplyr.tidyverse.org/reference/across Demonstrates how if_all() checks if a predicate function returns TRUE for all of the selected columns. This example checks if all columns in a:b are greater than 0. ```r library(dplyr) df <- tibble(a = c(1:2, NA), b = c(4:5, NA), c = c(7, 8, 9)) # Check if all columns in a:b are positive df %>% summarise(all_positive = if_all(a:b, ~ .x > 0)) ``` -------------------------------- ### Compute overlaps with specific bounds using join_by() Source: https://dplyr.tidyverse.org/reference/join_by This example shows how to compute overlaps between ranges when dealing with specific boundary types, such as right-open ranges '[)'. The 'bounds' argument in 'overlaps()' allows for precise overlap calculations. ```r by <- join_by(chromosome, overlaps(x$start, x$end, y$start, y$end, bounds = "[)")) full_join(segments, reference, by) ``` -------------------------------- ### Join by date range (rolling join) Source: https://dplyr.tidyverse.org/reference/join_by This example demonstrates a rolling join where each sale is matched with any promotion that occurred on or before the sale date, within the same ID. It uses an inequality condition within join_by(). ```r # For each `sale_date` within a particular `id`, # find all `promo_date`s that occurred before that particular sale by <- join_by(id, sale_date >= promo_date) left_join(sales, promos, by) ``` -------------------------------- ### Basic join by matching columns Source: https://dplyr.tidyverse.org/reference/join_by Use join_by() to match columns with the same name or different names across tables. This example joins sales data with promotional data based on 'id' and matching 'sale_date' to 'promo_date'. ```r sales <- tibble( id = c(1L, 1L, 1L, 2L, 2L), sale_date = as.Date(c("2018-12-31", "2019-01-02", "2019-01-05", "2019-01-04", "2019-01-01")) ) promos <- tibble( id = c(1L, 1L, 2L), promo_date = as.Date(c("2019-01-01", "2019-01-05", "2019-01-02")) ) # Match `id` to `id`, and `sale_date` to `promo_date` by <- join_by(id, sale_date == promo_date) left_join(sales, promos, by) ``` -------------------------------- ### Usage of all_vars and any_vars Source: https://dplyr.tidyverse.org/reference/all_vars Demonstrates the basic usage syntax for `all_vars()` and `any_vars()`. These functions are used to apply a predicate to variables. ```r all_vars(expr) any_vars(expr) ``` -------------------------------- ### Usage of top_n and top_frac Source: https://dplyr.tidyverse.org/reference/top_n Demonstrates the basic usage syntax for `top_n()` and `top_frac()` functions. ```r top_n(x, n, wt) top_frac(x, n, wt) ``` -------------------------------- ### Basic usage of when_any() and when_all() Source: https://dplyr.tidyverse.org/reference/when-any-all Compares the element-wise behavior of `when_any()` and `when_all()` with base R's `|` and `&` operators. Demonstrates how `when_any()` and `when_all()` work across multiple inputs simultaneously. ```r x <- c(TRUE, TRUE, TRUE, FALSE, FALSE, FALSE, NA, NA, NA) y <- c(TRUE, FALSE, NA, TRUE, FALSE, NA, TRUE, FALSE, NA) # `any()` and `all()` summarise down to 1 value any(x, y) #> [1] TRUE all(x, y) #> [1] FALSE # `when_any()` and `when_all()` work element by element across all inputs # at the same time. Their defaults are equivalent to calling `|` or `&`. when_any(x, y) #> [1] TRUE TRUE TRUE TRUE FALSE NA TRUE NA NA x | y #> [1] TRUE TRUE TRUE TRUE FALSE NA TRUE NA NA when_all(x, y) #> [1] TRUE FALSE NA FALSE FALSE FALSE NA FALSE NA x & y #> [1] TRUE FALSE NA FALSE FALSE FALSE NA FALSE NA ``` -------------------------------- ### Install duckplyr from R-universe Source: https://duckplyr.tidyverse.org/ Install the latest development version of duckplyr from the tidyverse R-universe repository. ```r install.packages("duckplyr", repos = c("https://tidyverse.r-universe.dev", "https://cloud.r-project.org")) ``` -------------------------------- ### Using purrr-style lambdas with across() Source: https://dplyr.tidyverse.org/reference/across Demonstrates using purrr-style lambda functions within across() for more complex transformations or to pass additional arguments. This example calculates the mean with NA removal. ```r library(dplyr) df <- tibble(a = c(1:2, NA), b = c(4:5, NA)) # Apply mean with na.rm = TRUE to columns a and b df %>% summarise(across(a:b, ~ mean(.x, na.rm = TRUE))) ``` -------------------------------- ### Install dtplyr from CRAN Source: https://dtplyr.tidyverse.org/ Install the dtplyr package from the Comprehensive R Archive Network (CRAN). ```r install.packages("dtplyr") ``` -------------------------------- ### Install duckplyr from CRAN Source: https://duckplyr.tidyverse.org/ Install the stable version of duckplyr from the Comprehensive R Archive Network (CRAN). ```r install.packages("duckplyr") ``` -------------------------------- ### Inequality Joins and NA Handling Source: https://dplyr.tidyverse.org/reference/mutate-joins Demonstrates how to perform joins based on inequality conditions and how to control the matching behavior of `NA` values. ```APIDOC ## Inequality Joins and NA Handling ### Description This section covers performing joins using inequality conditions (e.g., `>`) and managing how `NA` values are treated during joins. ### Method `*_join(x, y, by = join_by(...), na_matches = "left" | "never", ...)` ### Endpoint N/A (Function call in R) ### Parameters - **by** (expression) - Specifies the join keys using `join_by()`. Can use comparison operators other than `==`. - **na_matches** (character) - How to match `NA` values. Options are "left" (default, `NA` matches `NA`) or "never" (`NA` does not match `NA`). ### Request Example ```r # Inequality join df1 |> left_join(df2, join_by(x > x)) # NA values do not match left_join(df1, df2, na_matches = "never") ``` ``` -------------------------------- ### Explain Function Usage Source: https://dplyr.tidyverse.org/reference/explain Demonstrates the basic usage of the explain() function for inspecting objects. ```r explain(x, ...) ``` -------------------------------- ### Install dtplyr Development Version Source: https://dtplyr.tidyverse.org/ Install the development version of dtplyr from GitHub using the pak package manager. ```r # install.packages("pak") pak::pak("tidyverse/dtplyr") ``` -------------------------------- ### Basic grouping with group_by() Source: https://dplyr.tidyverse.org/reference/group_by Demonstrates how group_by() partitions a data frame. The output shows the data frame with an added grouping indicator. ```r by_cyl <- mtcars |> group_by(cyl) # grouping doesn't change how the data looks (apart from listing # how it's grouped): by_cyl ``` -------------------------------- ### Default behavior of when_any() and when_all() with zero inputs Source: https://dplyr.tidyverse.org/reference/when-any-all Compares the behavior of `any()` and `when_any()` (and `all()` and `when_all()`) when no inputs are provided. Shows that `when_any(size = 1)` returns FALSE and `when_all(size = 1)` returns TRUE, mirroring base R functions. ```r # When no inputs are provided, these functions are consistent with `any()` # and `all()` any() #> [1] FALSE when_any(size = 1) #> [1] FALSE all() #> [1] TRUE when_all(size = 1) #> [1] TRUE ``` -------------------------------- ### Basic Usage of lead() and lag() Source: https://dplyr.tidyverse.org/articles/window-functions Demonstrates the fundamental behavior of lead() and lag() functions by creating offset versions of a numeric vector. ```r x <- 1:5 lead(x) #> [1] 2 3 4 5 NA lag(x) #> [1] NA 1 2 3 4 ``` -------------------------------- ### Creating a wrapper for group_by() with pick() Source: https://dplyr.tidyverse.org/reference/pick Illustrates how to create a custom function 'my_group_by' that uses pick() to accept tidy-selection of columns for grouping. This demonstrates a bridge pattern between data-masking and tidy-select. ```r df <- tibble( x = c(3, 2, 2, 2, 1), y = c(0, 2, 1, 1, 4), z1 = c("a", "a", "a", "b", "a"), z2 = c("c", "d", "d", "a", "c") ) my_group_by <- function(data, cols) { group_by(data, pick({{ cols }})) } df |> my_group_by(c(x, starts_with("z"))) ``` -------------------------------- ### Find segments within reference ranges using join_by() Source: https://dplyr.tidyverse.org/reference/join_by This snippet demonstrates how to find every time a segment's 'start' falls between a reference's 'start' and 'end' range. It uses the 'between()' function within join_by() for the condition. ```r by <- join_by(chromosome, between(start, start, end)) full_join(segments, reference, by) ``` -------------------------------- ### Basic ntile() usage Source: https://dplyr.tidyverse.org/reference/ntile Demonstrates basic usage of ntile() with different numbers of buckets. Handles NA values by assigning them NA rank. ```r x <- c(5, 1, 3, 2, 2, NA) ntile(x, 2) #> [1] 2 1 2 1 1 NA ntile(x, 4) #> [1] 4 1 3 1 2 NA ``` -------------------------------- ### Filtering for one criterion Source: https://dplyr.tidyverse.org/reference/filter This example shows how to filter rows where the 'species' column is equal to 'Human'. ```APIDOC ## filter() ### Description Filter rows of a data frame. ### Usage ```r filter(.data, ..., .preserve = FALSE) ``` ### Arguments * `.data`: A data frame. * `...`: Logical expressions, each defining a condition for filtering. * `.preserve`: If `TRUE`, the grouping structure of `.data` is preserved. If `FALSE` (default), the result is ungrouped. ### Details `filter()` keeps only the rows that satisfy all the conditions specified in `...`. ### Examples ```r # Filtering for one criterion filter(starwars, species == "Human") ``` ``` -------------------------------- ### Basic Usage of cume_dist() and percent_rank() Source: https://dplyr.tidyverse.org/reference/percent_rank Demonstrates the direct application of cume_dist() and percent_rank() to a numeric vector. Shows the resulting proportional ranks. ```r x <- c(5, 1, 3, 2, 2) cume_dist(x) #> [1] 1.0 0.2 0.8 0.6 0.6 percent_rank(x) #> [1] 1.00 0.00 0.75 0.25 0.25 ``` -------------------------------- ### Count occurrences of a single variable Source: https://dplyr.tidyverse.org/reference/count Use `count()` to get a sense of the distribution of values in a dataset for a single variable. ```r starwars |> count(species) ``` -------------------------------- ### Get grouping variable names with group_vars() Source: https://dplyr.tidyverse.org/articles/grouping Use group_vars() to retrieve only the names of the grouping variables from a grouped data frame. ```r by_species |> group_vars() #> [1] "species" by_sex_gender |> group_vars() #> [1] "sex" "gender" ``` -------------------------------- ### across() examples Source: https://dplyr.tidyverse.org/reference/across Demonstrates various ways to use the across() function for applying operations to multiple columns, including using external vectors, purrr-style formulas, named lists of functions, and controlling output names with the .names argument. ```APIDOC ## across() ### Description Apply a function to multiple columns. ### Usage ```r across(.cols, .fns, ..., .names = NULL) ``` ### Arguments * `.cols`: Selection of columns to apply functions to. Defaults to all columns. * `.fns`: Functions to apply to the selected columns. Can be a single function, a list of functions, or a purrr-style formula. * `...`: Additional arguments to pass on to the functions in `.fns`. * `.names`: A glue specification that defines the names of the new columns. See `?glue` for details. The default is "{.col}" for a single function and "{.col}.{.fn}" for multiple functions. ### Examples # Using an external vector of names ```r library(dplyr) cols <- c("Sepal.Length", "Petal.Width") iris |> mutate(across(all_of(cols), round)) ``` # If the external vector is named, the output columns will be named according to those names ```r names(cols) <- tolower(cols) iris |> mutate(across(all_of(cols), round)) ``` # A purrr-style formula ```r iris |> group_by(Species) |> summarise(across(starts_with("Sepal"), ~ mean(.x, na.rm = TRUE))) ``` # A named list of functions ```r iris |> group_by(Species) |> summarise(across(starts_with("Sepal"), list(mean = mean, sd = sd))) ``` # Use the .names argument to control the output names ```r iris |> group_by(Species) |> summarise(across(starts_with("Sepal"), mean, .names = "mean_{.col}")) iris |> group_by(Species) |> summarise( across( starts_with("Sepal"), list(mean = mean, sd = sd), .names = "{.col}.{.fn}" ) ) ``` # If a named external vector is used for column selection, .names will use those names when constructing the output names ```r iris |> group_by(Species) |> summarise(across(all_of(cols), mean, .names = "mean_{.col}")) ``` # When the list is not named, .fn is replaced by the function's position ```r iris |> group_by(Species) |> summarise( across(starts_with("Sepal"), list(mean, sd), .names = "{.col}.fn{.fn}") ) ``` ``` -------------------------------- ### Basic select usage Source: https://dplyr.tidyverse.org/reference/select Demonstrates the basic syntax for using the select function to choose columns from a data frame. ```r select(.data, ...) ``` -------------------------------- ### Select rows by position with slice() Source: https://dplyr.tidyverse.org/articles/dplyr Use slice() to select rows by their integer locations. This example selects rows 5 through 10. ```r starwars |> slice(5:10) ``` -------------------------------- ### arrange() with Locale Source: https://dplyr.tidyverse.org/reference/arrange Illustrates how to use arrange() with different locales for sorting character vectors. Requires the stringi package for non-C locales. ```r library(dplyr) # Example data with mixed case characters char_df <- tibble(x = c("a", "B", "c", "D")) # Arrange using the default "C" locale (often faster) print(arrange(char_df, x, .locale = "C")) # Arrange using the American English locale (requires stringi) # print(arrange(char_df, x, .locale = "en")) ``` -------------------------------- ### Create a tibble for Likert scale scores Source: https://dplyr.tidyverse.org/articles/recoding-replacing Initializes a tibble with numeric Likert scale scores to be used in subsequent recoding examples. ```r likert <- tibble( score = c(1, 2, 3, 4, 5, 2, 3, 1, 4) ) ``` -------------------------------- ### Inner Join Example Source: https://dplyr.tidyverse.org/articles/two-table Performs an inner join on df1 and df2, including only observations that match in both tibbles based on column 'x'. ```r df1 |> inner_join(df2) |> knitr::kable() ``` -------------------------------- ### Show Query Function Usage Source: https://dplyr.tidyverse.org/reference/explain Demonstrates the basic usage of the show_query() function, likely for displaying database queries. ```r show_query(x, ...) ``` -------------------------------- ### Distinct rows based on a single column Source: https://dplyr.tidyverse.org/reference/distinct Shows how to use distinct() to get unique rows based on the values in a single specified column. ```r distinct(df, x) #> # A tibble: 10 × 1 #> x #> #> 1 10 #> 2 5 #> 3 8 #> 4 3 #> 5 2 #> 6 6 #> 7 4 #> 8 1 #> 9 7 #> 10 9 distinct(df, y) #> # A tibble: 10 × 1 #> y #> #> 1 7 #> 2 10 #> 3 4 #> 4 8 #> 5 9 #> 6 5 #> 7 6 #> 8 1 #> 9 2 #> 10 3 ``` -------------------------------- ### Manual Calculation of cume_dist() and percent_rank() Source: https://dplyr.tidyverse.org/reference/percent_rank Illustrates how to manually compute the values equivalent to cume_dist() and percent_rank() using base R functions. This helps in understanding the underlying logic. ```r # You can understand what's going on by computing it by hand sapply(x, function(xi) sum(x <= xi) / length(x)) #> [1] 1.0 0.2 0.8 0.6 0.6 sapply(x, function(xi) sum(x < xi) / (length(x) - 1)) #> [1] 1.00 0.00 0.75 0.25 0.25 # The real computations are a little more complex in order to # correctly deal with missing values ``` -------------------------------- ### Adding multiple columns with reframe() Source: https://dplyr.tidyverse.org/reference/reframe Return a data frame from the applied function to add multiple columns at once. This example calculates quantiles for a vector. ```r quantile_df <- function(x, probs = c(0.25, 0.5, 0.75)) { tibble( val = quantile(x, probs, na.rm = TRUE), quant = probs ) } x <- c(10, 15, 18, 12) quantile_df(x) ``` -------------------------------- ### Basic Mutate Joins Source: https://dplyr.tidyverse.org/reference/mutate-joins Demonstrates the fundamental usage of inner_join, left_join, right_join, and full_join with default join keys. ```APIDOC ## inner_join ### Description Combines two data frames by matching rows based on common keys. Only rows with matching keys in both data frames are kept. ### Method `inner_join(x, y, by = NULL, ...)` ### Endpoint N/A (Function call in R) ### Parameters - **x** (data.frame) - The first data frame. - **y** (data.frame) - The second data frame. - **by** (character or list) - Variables to join by. If NULL, uses all variables in common. ### Request Example ```r band_members |> inner_join(band_instruments) ``` ## left_join ### Description Combines two data frames by matching rows based on common keys. Keeps all rows from the first data frame (`x`) and matching rows from the second data frame (`y`). Unmatched rows in `y` will have `NA` values for columns from `y`. ### Method `left_join(x, y, by = NULL, ...)` ### Endpoint N/A (Function call in R) ### Parameters - **x** (data.frame) - The first data frame. - **y** (data.frame) - The second data frame. - **by** (character or list) - Variables to join by. If NULL, uses all variables in common. ### Request Example ```r band_members |> left_join(band_instruments) ``` ## right_join ### Description Combines two data frames by matching rows based on common keys. Keeps all rows from the second data frame (`y`) and matching rows from the first data frame (`x`). Unmatched rows in `x` will have `NA` values for columns from `x`. ### Method `right_join(x, y, by = NULL, ...)` ### Endpoint N/A (Function call in R) ### Parameters - **x** (data.frame) - The first data frame. - **y** (data.frame) - The second data frame. - **by** (character or list) - Variables to join by. If NULL, uses all variables in common. ### Request Example ```r band_members |> right_join(band_instruments) ``` ## full_join ### Description Combines two data frames by matching rows based on common keys. Keeps all rows from both data frames (`x` and `y`). Unmatched rows will have `NA` values for columns from the data frame where the match did not occur. ### Method `full_join(x, y, by = NULL, ...)` ### Endpoint N/A (Function call in R) ### Parameters - **x** (data.frame) - The first data frame. - **y** (data.frame) - The second data frame. - **by** (character or list) - Variables to join by. If NULL, uses all variables in common. ### Request Example ```r band_members |> full_join(band_instruments) ``` ``` -------------------------------- ### Select rows by position with slice() Source: https://dplyr.tidyverse.org/articles/base Use slice() to select rows by their position in the data frame. This example selects rows from 25 to the end. ```r slice(mtcars, 25:n()) #> # A tibble: 8 × 13 #> mpg cyl disp hp drat wt qsec vs am gear carb #> #> 1 19.2 8 400 175 3.08 3.84 17.0 0 0 3 2 #> 2 27.3 4 79 66 4.08 1.94 18.9 1 1 4 1 #> 3 26 4 120. 91 4.43 2.14 16.7 0 1 5 2 #> 4 30.4 4 95.1 113 3.77 1.51 16.9 1 1 5 2 #> # ℹ 4 more rows #> # ℹ 2 more variables: cyl2 , cyl4 ``` -------------------------------- ### Create a sample tibble Source: https://dplyr.tidyverse.org/articles/rowwise This snippet creates a sample tibble with integer columns to demonstrate row-wise operations. ```r df <- tibble(id = 1:6, w = 10:15, x = 20:25, y = 30:35, z = 40:45) df ``` -------------------------------- ### Relocate columns with relocate() in R Source: https://dplyr.tidyverse.org/articles/dplyr Use relocate() to change the order of columns. This example moves a range of columns to appear before the 'height' column. ```r starwars |> relocate(sex:homeworld, .before = height) ``` -------------------------------- ### Explain Query Plan Source: https://duckplyr.tidyverse.org/ Displays the query plan for the 'out' data frame, showing the sequence of operations like HASH_GROUP_BY, PROJECTION, and READ_PARQUET. This is useful for understanding query optimization and execution. ```R out |> explain() ``` -------------------------------- ### Programmatic Column Selection with `all_of()` in R Source: https://dplyr.tidyverse.org/articles/dplyr Demonstrates how to programmatically select columns using `all_of()` with a character vector of column names, alongside a directly specified column name. ```r vars <- c("name", "height") select(starwars, all_of(vars), "mass") #> # A tibble: 87 × 3 #> name height mass #> #> 1 Luke Skywalker 172 77 #> 2 C-3PO 167 75 #> 3 R2-D2 96 32 #> 4 Darth Vader 202 136 #> # ℹ 83 more rows ``` -------------------------------- ### Plot Recent Storm Paths Source: https://dplyr.tidyverse.org/reference/storms Visualizes storm paths for storms from the year 2000 onwards using ggplot2. Requires the ggplot2 package to be installed. ```r if (requireNamespace("ggplot2", quietly = TRUE)) { library(ggplot2) storms |> filter(year >= 2000) | ggplot(aes(long, lat, color = paste(year, name))) + geom_path(show.legend = FALSE) + facet_wrap(~year) } ``` -------------------------------- ### Get group indices with group_indices() Source: https://dplyr.tidyverse.org/articles/grouping Use group_indices() to determine which group each row belongs to. The output is a vector of integers representing the group index for each row. ```r by_species |> group_indices() #> [1] 11 6 6 11 11 11 11 6 11 11 11 11 34 11 24 12 11 38 36 11 11 6 #> [23] 31 11 11 18 11 11 8 26 11 21 11 11 10 10 10 11 30 7 11 11 37 32 #> [45] 32 1 33 35 29 11 3 20 37 27 13 23 16 4 38 38 11 9 17 17 11 11 #> [67] 11 11 5 2 15 15 11 6 25 19 28 14 34 11 38 22 11 11 11 6 11 ``` -------------------------------- ### arrange() with Grouping Source: https://dplyr.tidyverse.org/reference/arrange Shows how to use arrange() with grouped data frames. By default, grouping is ignored unless .by_group = TRUE is specified. ```r library(dplyr) # Example grouped data frame grouped_df <- tibble( g = c("A", "A", "B", "B"), x = c(1, 2, 1, 2) ) %>% group_by(g) # Arrange without .by_group (ignores grouping) print(arrange(grouped_df, x)) # Arrange with .by_group = TRUE (sorts by group first) print(arrange(grouped_df, .by_group = TRUE, x)) ``` -------------------------------- ### Step-by-step data manipulation Source: https://dplyr.tidyverse.org/articles/dplyr This demonstrates a step-by-step approach to data manipulation, naming intermediate results. It requires explicit function calls for each operation. ```r a1 <- group_by(starwars, species, sex) a2 <- select(a1, height, mass) a3 <- summarise( a2, height = mean(height, na.rm = TRUE), mass = mean(mass, na.rm = TRUE) ) ``` -------------------------------- ### Add new column with mutate() in R Source: https://dplyr.tidyverse.org/articles/dplyr Use mutate() to add a new column that is a function of existing columns. This example calculates height in meters. ```r starwars |> mutate(height_m = height / 100) ``` -------------------------------- ### Full Join Example Source: https://dplyr.tidyverse.org/articles/two-table Performs a full join on df1 and df2, including all observations from both tibbles. Non-matching rows will have NA values for columns from the other tibble. ```r df1 |> full_join(df2) ``` -------------------------------- ### Programmatic tibble creation with environment variable name Source: https://dplyr.tidyverse.org/articles/programming Demonstrates creating a tibble column with a name stored in an environment variable using glue syntax and `:=`. ```r name <- "susan" tibble("{name}" := 2) #> # A tibble: 1 × 1 #> susan #> #> 1 2 ``` -------------------------------- ### Glimpse a data frame (mtcars) Source: https://dplyr.tidyverse.org/reference/glimpse Use glimpse() to get a compact summary of the mtcars dataset, showing column names, data types, and a preview of the data. ```r glimpse(mtcars) ``` -------------------------------- ### Basic Usage of order_by() Source: https://dplyr.tidyverse.org/reference/order_by Demonstrates the fundamental usage of `order_by()` with numeric vectors. This snippet shows how to specify an ordering vector and a function call to achieve a custom ordering for the result. ```r order_by(10:1, cumsum(1:10)) ``` ```r x <- 10:1 y <- 1:10 order_by(x, cumsum(y)) ``` -------------------------------- ### Filtering for multiple criteria Source: https://dplyr.tidyverse.org/reference/filter This example demonstrates filtering rows based on multiple conditions combined with logical OR (|) and AND (&) operators, as well as comma-separated expressions which are implicitly ANDed. ```APIDOC ## filter() ### Description Filter rows of a data frame. ### Usage ```r filter(.data, ..., .preserve = FALSE) ``` ### Details Multiple comma separated expressions are combined using `&`. ### Examples ```r # Filtering for multiple criteria within a single logical expression filter(starwars, hair_color == "none" & eye_color == "black") filter(starwars, hair_color == "none" | eye_color == "black") # Multiple comma separated expressions are combined using `&` starwars |> filter(hair_color == "none", eye_color == "black") ``` ``` -------------------------------- ### Uneven bucket sizes with ntile() Source: https://dplyr.tidyverse.org/reference/ntile Illustrates how ntile() handles uneven bucket sizes when the number of elements is not an integer multiple of n. Larger buckets appear first. ```r # If the bucket sizes are uneven, the larger buckets come first ntile(1:8, 3) #> [1] 1 1 1 2 2 2 3 3 ``` -------------------------------- ### Select first N rows with slice_head() Source: https://dplyr.tidyverse.org/articles/dplyr Use slice_head() to select the first N rows of a data frame. This example selects the first 3 rows. ```r starwars |> slice_head(n = 3) ``` -------------------------------- ### Basic arrange() Usage Source: https://dplyr.tidyverse.org/reference/arrange Demonstrates the basic usage of arrange() to order rows of a data frame by specified columns. Use desc() for descending order. ```r library(dplyr) # Example data frame df <- tibble( x = c(1, 3, 2), y = c("a", "c", "b") ) # Arrange by 'x' in ascending order arrangements <- list( arrange(df, x), arrange(df, desc(x)), arrange(df, x, y) ) # Print arrangements for (arr in arrangements) { print(arr) } ``` -------------------------------- ### if_else() vs case_when() Syntax Source: https://dplyr.tidyverse.org/articles/recoding-replacing Illustrates the syntax of if_else() and case_when() for conditional recoding. ```r if_else(condition, true, false, missing) case_when( condition ~ true, !condition ~ false, is.na(condition) ~ missing ) ``` -------------------------------- ### Overwrite grouping variables with group_by() Source: https://dplyr.tidyverse.org/articles/grouping Applying group_by() to an already grouped dataset overwrites the existing grouping variables. This example groups by 'homeworld' instead of 'species'. ```r by_species | group_by(homeworld) | tally() #> # A tibble: 49 × 2 #> homeworld n #> #> 1 Alderaan 3 #> 2 Aleen Minor 1 #> 3 Bespin 1 #> 4 Bestine IV 1 #> # ℹ 45 more rows ``` -------------------------------- ### Select first N rows with slice_head Source: https://dplyr.tidyverse.org/reference/slice Use slice_head to select the first N rows of a data frame. This is useful for quickly viewing the start of your data. ```r mtcars |> slice_head(n = 5) ``` -------------------------------- ### Using when_any() with filter() Source: https://dplyr.tidyverse.org/reference/when-any-all Demonstrates how `when_any()` simplifies complex filtering conditions within `filter()`. It replaces explicit OR logic (`|`) with a more readable, indented structure. ```r countries <- tibble( name = c("US", "CA", "PR", "RU", "US", NA, "CA", "PR", "RU"), score = c(200, 100, 150, NA, 50, 100, 300, 250, 120) ) countries #> # A tibble: 9 × 2 #> name score #> #> 1 US 200 #> 2 CA 100 #> 3 PR 150 #> 4 RU NA #> 5 US 50 #> 6 NA 100 #> 7 CA 300 #> 8 PR 250 #> 9 RU 120 # With `when_any()`, you drop the explicit `|`, the extra `()`, and your # conditions are all indented to the same level countries |> filter(when_any( name %in% c("US", "CA") & between(score, 200, 300), name %in% c("PR", "RU") & between(score, 100, 200) )) #> # A tibble: 4 × 2 #> name score #> #> 1 US 200 #> 2 PR 150 #> 3 CA 300 #> 4 RU 120 ``` -------------------------------- ### Sort by multiple columns in descending order Source: https://dplyr.tidyverse.org/reference/arrange Sorts the 'iris' dataset by columns starting with 'Sepal' in descending order. Requires the dplyr package and the iris dataset. ```r iris |> arrange(across(starts_with("Sepal"), desc)) ``` -------------------------------- ### Use glimpse() in a data pipeline Source: https://dplyr.tidyverse.org/reference/glimpse Demonstrates how glimpse() can be used within a data pipeline, returning the original data invisibly, allowing subsequent operations like select(). ```r mtcars |> glimpse() |> select(1:3) ``` -------------------------------- ### Load tidyverse and prepare iris dataset Source: https://dplyr.tidyverse.org/reference/select Loads the tidyverse package and converts the iris dataset to a tibble for easier manipulation. This is a common setup for dplyr operations. ```r library(tidyverse) # For better printing iris <- as_tibble(iris) ``` -------------------------------- ### Rolling join with a time window Source: https://dplyr.tidyverse.org/reference/join_by This example demonstrates a rolling join with an additional constraint: the promotion must have occurred within a specific time window (at most 1 day before the sale). It uses multiple conditions within join_by() and a full_join to show unmatched rows. ```r # Same as before, but also require that the promo had to occur at most 1 # day before the sale was made. We'll use a full join to see that id 2's # promo on `2019-01-02` is no longer matched to the sale on `2019-01-04`. sales <- mutate(sales, sale_date_lower = sale_date - 1) by <- join_by(id, closest(sale_date >= promo_date), sale_date_lower <= promo_date) full_join(sales, promos, by) ``` -------------------------------- ### Using cur_column() within across() in R Source: https://dplyr.tidyverse.org/reference/context Demonstrates how to use cur_column() to get the name of the current column being processed by across() within mutate(). The data must be grouped. ```r gf |> mutate(across(everything(), ~ paste(cur_column(), round(.x, 2)))) ``` -------------------------------- ### Calculate row counts per group with summarise() and pick() Source: https://dplyr.tidyverse.org/articles/rowwise Demonstrates the recommended way to calculate the number of rows per group using `summarise()` and `pick(everything())`. This avoids the automatic list wrapping of the older `do()` function. ```r mtcars | group_by(cyl) | summarise(nrows = nrow(pick(everything()))) ``` -------------------------------- ### Filter for a single criterion in R Source: https://dplyr.tidyverse.org/reference/filter Use filter() to select rows where a specific column meets a condition. This example filters the starwars dataset for rows where the species is 'Human'. ```r filter(starwars, species == "Human") ```