### Example Data Setup for dplyr Joins Source: https://cran.r-project.org/web/packages/dplyr/refman/dplyr.html/index Sets up two tibbles, 'sales' and 'promos', with date and ID columns. These tibbles are used in subsequent examples to demonstrate different types of joins in dplyr. ```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")) ) sales promos <- tibble( id = c(1L, 1L, 2L), promo_date = as.Date(c("2019-01-01", "2019-01-05", "2019-01-02")) ) promos ``` -------------------------------- ### Example Data Setup for Range Joins with dplyr Source: https://cran.r-project.org/web/packages/dplyr/refman/dplyr.html/index Sets up two tibbles, 'segments' and 'reference', containing genomic segment data with chromosome, start, and end positions. These are used to demonstrate range-based joins. ```r segments <- tibble( segment_id = 1:4, chromosome = c("chr1", "chr2", "chr2", "chr1"), start = c(140, 210, 380, 230), end = c(150, 240, 415, 280) ) segments reference <- tibble( reference_id = 1:4, chromosome = c("chr1", "chr1", "chr2", "chr2"), start = c(100, 200, 300, 415), end = c(150, 250, 399, 450) ) reference ``` -------------------------------- ### Summarise_at example with grouping variables Source: https://cran.r-project.org/web/packages/dplyr/refman/dplyr.html/index Provides an example of using `summarise_at` while explicitly excluding grouping variables using `vars(-group_cols())` to prevent errors and ensure correct application of the summary function. ```R data %> summarise_at(vars(-group_cols(), ...), myoperation) ``` -------------------------------- ### Example: Connecting to SQLite and Creating Tables in R Source: https://cran.r-project.org/web/packages/dplyr/refman/dplyr.html/index This example demonstrates how to connect to an in-memory SQLite database using `DBI::dbConnect()`, copy the `mtcars` dataset into it using `copy_to()`, and then retrieve the table using `tbl()`. It also shows how to query using raw SQL with `sql()`. ```R con <- DBI::dbConnect(RSQLite::SQLite(), ":memory:") copy_to(con, mtcars) # To retrieve a single table from a source, use `tbl()` mtcars <- con %>% tbl("mtcars") mtcars # You can also use pass raw SQL if you want a more sophisticated query con %>% tbl(sql("SELECT * FROM mtcars WHERE cyl == 8")) ``` -------------------------------- ### Superseded do() function examples in R Source: https://cran.r-project.org/web/packages/dplyr/refman/dplyr.html/index Demonstrates the usage of the superseded do() function in R, explaining its replacement by reframe(), nest_by(), and pick(). Examples show how do() with unnamed and named arguments translates to newer functions. ```r # do() with unnamed arguments becomes reframe() or summarise() # . becomes pick() by_cyl <- mtcars %>% group_by(cyl) by_cyl %>% do(head(., 2)) # -> by_cyl %>% reframe(head(pick(everything()), 2)) by_cyl %>% slice_head(n = 2) # Can refer to variables directly by_cyl %>% do(mean = mean(.$vs)) # -> by_cyl %>% summarise(mean = mean(vs)) # do() with named arguments becomes nest_by() + mutate() & list() models <- by_cyl %>% do(mod = lm(mpg ~ disp, data = .)) # -> models <- mtcars %>% nest_by(cyl) %>% mutate(mod = list(lm(mpg ~ disp, data = data))) models %>% summarise(rsq = summary(mod)$r.squared) # use broom to turn models into data models %>% do(data.frame( var = names(coef(.$mod)), coef(summary(.$mod))) ) # -> models %>% reframe(broom::tidy(mod)) ``` -------------------------------- ### Visualize recent storm paths with ggplot2 Source: https://cran.r-project.org/web/packages/dplyr/refman/dplyr.html/index This example visualizes recent storm paths using the 'storms' dataset and the ggplot2 package. It filters storms from the year 2000 onwards and plots their latitude and longitude, faceted by year. Requires the ggplot2 package to be installed. ```r # Show a few recent storm paths 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) } ``` -------------------------------- ### Example: Scaling Height and Mass using mutate_at in dplyr (Superseded) Source: https://cran.r-project.org/web/packages/dplyr/refman/dplyr.html/index Demonstrates how to use the superseded `mutate_at()` function to apply a custom scaling function (`scale2`) to the 'height' and 'mass' columns of the 'starwars' dataset. It also shows how to pass additional arguments to the scaling function. ```r scale2 <- function(x, na.rm = FALSE) (x - mean(x, na.rm = na.rm)) / sd(x, na.rm) starwars %>% mutate_at(c("height", "mass"), scale2) # With additional arguments starwars %>% mutate_at(c("height", "mass"), scale2, na.rm = TRUE) # Using purrr-style lambda starwars %>% mutate_at(c("height", "mass"), ~scale2(., na.rm = TRUE)) ``` -------------------------------- ### Example Datasets: Beatles and Rolling Stones Band Members Source: https://cran.r-project.org/web/packages/dplyr/refman/dplyr.html/index These are toy datasets (`band_members`, `band_instruments`, `band_instruments2`) designed for demonstrating data manipulation techniques, particularly joins. They contain information about band members and their instruments. `band_instruments` and `band_instruments2` have identical data but differ in the naming of the first column, affecting join key matching. ```R band_members band_instruments band_instruments2 ``` -------------------------------- ### Create expenses tibble for dplyr examples Source: https://cran.r-project.org/web/packages/dplyr/refman/dplyr.html/index This code snippet creates a sample tibble named 'expenses' which is used in subsequent examples to demonstrate dplyr's grouping functionalities. It includes columns for 'id', 'region', and 'cost'. ```r expenses <- tibble( id = c(1, 2, 1, 3, 1, 2, 3), region = c("A", "A", "A", "B", "B", "A", "A"), cost = c(25, 20, 19, 12, 9, 6, 6) ) expenses ``` -------------------------------- ### Mutate: Window Functions for Grouped Data (R) Source: https://cran.r-project.org/web/packages/dplyr/refman/dplyr.html/index Demonstrates the use of window functions within mutate() on grouped data. This example calculates the rank of 'mass' within each 'homeworld' group, showcasing how mutate() interacts with grouping and ranking. ```r starwars %> select(name, mass, homeworld) %> group_by(homeworld) %> mutate(rank = min_rank(desc(mass))) ``` -------------------------------- ### Load tidyverse and prepare data Source: https://cran.r-project.org/web/packages/dplyr/refman/dplyr.html/index Loads the tidyverse library and converts the 'iris' dataset to a tibble for enhanced printing. This is a common setup step for using dplyr functions. ```r library(tidyverse) # For better printing iris <- as_tibble(iris) ``` -------------------------------- ### Sampling Rows with dplyr (R) Source: https://cran.r-project.org/web/packages/dplyr/refman/dplyr.html/index Demonstrates how to sample rows from a data frame using dplyr's sample_n and sample_frac functions, and their modern equivalents slice_sample. It shows sampling by count, fraction, with replacement, and using weights. The examples highlight differences in behavior, especially when sample sizes exceed group sizes. ```R df <- tibble(x = 1:5, w = c(0.1, 0.1, 0.1, 2, 2)) # sample_n() -> slice_sample() ---------------------------------------------- # Was: sample_n(df, 3) sample_n(df, 10, replace = TRUE) sample_n(df, 3, weight = w) # Now: slice_sample(df, n = 3) slice_sample(df, n = 10, replace = TRUE) slice_sample(df, n = 3, weight_by = w) # Note that sample_n() would error if n was bigger than the group size # slice_sample() will just use the available rows for consistency with # the other slice helpers like slice_head() try(sample_n(df, 10)) slice_sample(df, n = 10) # sample_frac() -> slice_sample() ------------------------------------------- # Was: sample_frac(df, 0.25) sample_frac(df, 2, replace = TRUE) # Now: slice_sample(df, prop = 0.25) slice_sample(df, prop = 2, replace = TRUE) ``` -------------------------------- ### Basic Equality Join with dplyr Source: https://cran.r-project.org/web/packages/dplyr/refman/dplyr.html/index Performs a left join between 'sales' and 'promos' tibbles, matching on both 'id' and 'sale_date' being equal to 'promo_date'. This is a fundamental example of using `join_by()` for exact matches. ```r # Match `id` to `id`, and `sale_date` to `promo_date` by <- join_by(id, sale_date == promo_date) left_join(sales, promos, by) ``` -------------------------------- ### Summarise_at example excluding group names Source: https://cran.r-project.org/web/packages/dplyr/refman/dplyr.html/index Illustrates an alternative method for `summarise_at` by manually removing grouping variable names from a character vector before applying the summary function, ensuring grouping variables are not processed. ```R nms <- setdiff(nms, group_vars(data)) data %>% summarise_at(nms, myoperation) ``` -------------------------------- ### Apply function to multiple columns with dplyr::across() Source: https://cran.r-project.org/web/packages/dplyr/refman/dplyr.html/index Demonstrates the basic usage of `across()` to apply functions to selected columns. It shows how to select columns using various tidyselect methods and apply a single function (e.g., `round`) or a list of functions (e.g., `mean`, `sd`). The examples also illustrate how to use purrr-style formulas and named lists of functions for more complex transformations. ```r iris <- as_tibble(iris) # Different ways to select the same set of columns iris %>% mutate(across(c(Sepal.Length, Sepal.Width), round)) iris %>% mutate(across(c(1, 2), round)) iris %>% mutate(across(1:Sepal.Width, round)) iris %>% mutate(across(where(is.double) & !c(Petal.Length, Petal.Width), round)) # Using an external vector of names 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 names(cols) <- tolower(cols) iris %>% mutate(across(all_of(cols), round)) # A purrr-style formula iris %>% group_by(Species) %>% summarise(across(starts_with("Sepal"), ~ mean(.x, na.rm = TRUE))) # A named list of functions iris %>% group_by(Species) %>% summarise(across(starts_with("Sepal"), list(mean = mean, sd = sd))) ``` -------------------------------- ### Example: Scaling Height and Mass using across in dplyr Source: https://cran.r-project.org/web/packages/dplyr/refman/dplyr.html/index Illustrates the modern approach using `across()` to apply a custom scaling function (`scale2`) to the 'height' and 'mass' columns of the 'starwars' dataset. This replaces the superseded `mutate_at()` function and demonstrates passing additional arguments and using lambda functions. ```r scale2 <- function(x, na.rm = FALSE) (x - mean(x, na.rm = na.rm)) / sd(x, na.rm) # Equivalent to the mutate_at example starwars %>% mutate(across(c("height", "mass"), scale2)) # With additional arguments starwars %>% mutate(across(c("height", "mass"), scale2, na.rm = TRUE)) # Using purrr-style lambda starwars %>% mutate(across(c("height", "mass"), ~ scale2(.x, na.rm = TRUE))) ``` -------------------------------- ### Arrange Data with Different Locales in R Source: https://cran.r-project.org/web/packages/dplyr/refman/dplyr.html/index Demonstrates how to use the `arrange()` function in R's dplyr package to sort character data according to different locale settings. It shows the default 'C' locale behavior, the English locale ('en'), and Danish locale ('da'), highlighting case sensitivity and special character sorting. It also includes an example of temporarily reverting to legacy behavior using `dplyr.legacy_locale`. ```r df <- tibble(x = c("a", "b", "C", "B", "c")) df # Default locale is C, which groups the English alphabet by case, placing # uppercase letters before lowercase letters. arrange(df, x) # The American English locale groups the alphabet by letter. # Explicitly override `.locale` with "en" for this ordering. arrange(df, x, .locale = "en") # This Danish letter is expected to sort after `z` df <- tibble(x = c("o", "p", "\u00F8", "z")) df # The American English locale sorts it right after `o` arrange(df, x, .locale = "en") # Using "da" for Danish ordering gives the expected result arrange(df, x, .locale = "da") # If you need the legacy behavior of `arrange()`, which respected the # system locale, then you can set the global option `dplyr.legacy_locale`, # but expect this to be removed in the future. We recommend that you use # the `.locale` argument instead. rlang::with_options(dplyr.legacy_locale = TRUE, { arrange(df, x) }) ``` -------------------------------- ### Between Join with dplyr Source: https://cran.r-project.org/web/packages/dplyr/refman/dplyr.html/index Finds all reference ranges that contain the start of a segment. This uses the `between()` helper function within `join_by()` to check if a segment's start falls within a reference's start and end. ```r # Find every time a segment `start` falls between the reference # `[start, end]` range. by <- join_by(chromosome, between(start, start, end)) full_join(segments, reference, by) ``` -------------------------------- ### Emulate recode_factor() with case_match() and .ptype Source: https://cran.r-project.org/web/packages/dplyr/refman/dplyr.html/index Shows how to achieve the functionality of recode_factor() using case_match() by specifying the replacements, default, missing value handling, and crucially, the .ptype argument to define the output as an ordered factor with specific levels. ```R num_vec <- c(1:4, NA) case_match( num_vec, 1 ~ "z", 2 ~ "y", 3 ~ "x", NA ~ "M", .default = "D", .ptype = factor(levels = c("z", "y", "x", "D", "M")) ) ``` -------------------------------- ### Explain Database Query Plans with dbplyr Source: https://cran.r-project.org/web/packages/dplyr/refman/dplyr.html/index The explain() and show_query() functions from the dbplyr package provide details about database objects and query plans. explain() offers human-readable output, while show_query() displays the SQL query. For tbl_sql objects, explain() runs the SQL EXPLAIN command to describe the query execution plan, aiding in performance diagnosis. ```R explain(x, ...) show_query(x, ...) # Example usage: lahman_s <- dbplyr::lahman_sqlite() batting <- tbl(lahman_s, "Batting") batting %>% show_query() batting %>% explain() # Filtering and explaining query plan: batting %>% filter(lgID == "NL" & yearID == 2000L) %>% explain() ``` -------------------------------- ### Arrange All Columns in a Data Frame (R) Source: https://cran.r-project.org/web/packages/dplyr/refman/dplyr.html/index Demonstrates how to arrange all columns of a data frame using `arrange_all` and `across` functions in R. It shows basic usage and sorting in descending order. ```R df <- as_tibble(mtcars) arrange_all(df) # -> arrange(df, pick(everything())) arrange_all(df, desc) # -> arrange(df, across(everything(), desc)) ``` -------------------------------- ### Mutate: Creating and Modifying Columns (R) Source: https://cran.r-project.org/web/packages/dplyr/refman/dplyr.html/index Illustrates how to use mutate() to create new columns based on existing ones and how to modify existing columns. New columns are available for subsequent calculations within the same mutate() call. ```r starwars %> select(name, mass) %> mutate( mass2 = mass * 2, mass2_squared = mass2 * mass2 ) starwars %> select(name, height, mass, homeworld) %> mutate( mass = NULL, height = height * 0.0328084 # convert to feet ) ``` -------------------------------- ### Get Current Group/Variable Data (Deprecated) Source: https://cran.r-project.org/web/packages/dplyr/refman/dplyr.html/index These functions were used to access the current data or grouping variables within dplyr operations. They have been deprecated in favor of `pick()` or do not have direct replacements due to ambiguity. ```R cur_data() cur_data_all() ``` -------------------------------- ### Create a table from a data source Source: https://cran.r-project.org/web/packages/dplyr/refman/dplyr.html/index This is a generic method that dispatches based on the first argument. ```APIDOC ## tbl() ### Description Creates a table from a data source. ### Method `tbl(src, ...)` `is.tbl(x)` ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body - `src` (data source) - A data source. - `...` - Other arguments passed on to the individual methods. - `x` (any) - Any object. ### Request Example ```r # Example for tbl my_data <- data.frame(x = 1:5, y = letters[1:5]) dplyr::tbl(my_data) # Example for is.tbl dplyr::is.tbl(my_data) ``` ### Response #### Success Response (200) Returns a tibble or a logical value indicating if an object is a tibble. #### Response Example ```r # Example response for tbl # A tibble: 5 x 2 x y 1 1 a 2 2 b 3 3 c 4 4 d 5 5 e # Example response for is.tbl # TRUE ``` ``` -------------------------------- ### Demonstrate distinct() variations in R Source: https://cran.r-project.org/web/packages/dplyr/refman/dplyr.html/index Illustrates different ways to use the distinct() function in R, including distinct_all(), distinct_at(), and distinct_if(). It shows how to pick specific columns or apply functions before extracting distinct values. ```r df <- tibble(x = rep(2:5, each = 2) / 2, y = rep(2:3, each = 4) / 2) distinct_all(df) # -> distinct(df, pick(everything())) distinct_at(df, vars(x,y)) # -> distinct(df, pick(x, y)) distinct_if(df, is.numeric) # -> distinct(df, pick(where(is.numeric))) # You can supply a function that will be applied before extracting the distinct values # The variables of the sorted tibble keep their original values. distinct_all(df, round) # -> distinct(df, across(everything(), round)) ``` -------------------------------- ### Create a 'src' object Source: https://cran.r-project.org/web/packages/dplyr/refman/dplyr.html/index `src()` is the standard constructor for srcs and `is.src()` tests for the existence of a src object. ```APIDOC ## Create a "src" object ### Description `src()` is the standard constructor for srcs and `is.src()` tests. ### Usage ```R src(subclass, ...) is.src(x) ``` ### Arguments `subclass` | name of subclass. "src" is an abstract base class, so you must supply this value. `src_` is automatically prepended to the class name `...` | fields used by object. These dots are evaluated with explicit splicing. `x` | object to test for "src"-ness. ``` -------------------------------- ### Strict Inequality Closest Match Join with dplyr Source: https://cran.r-project.org/web/packages/dplyr/refman/dplyr.html/index Similar to the previous example, but uses a strict inequality (`>`) to exclude exact date matches. This ensures that the closest match must be strictly before the sale date. ```r # If you want to disallow exact matching in rolling joins, use `>` rather # than `>=`. Note that the promo on `2019-01-05` is no longer considered the # closest match for the sale on the same date. by <- join_by(id, closest(sale_date > promo_date)) left_join(sales, promos, by) ``` -------------------------------- ### Explicit Column Referencing with Between Join in dplyr Source: https://cran.r-project.org/web/packages/dplyr/refman/dplyr.html/index Demonstrates how to explicitly specify columns from the left ('x') and right ('y') tables using `x$` and `y$` prefixes within `join_by()`. This example performs the same `between` join as the previous one but with explicit referencing. ```r # If you wanted the reference columns first, supply `reference` as `x` # and `segments` as `y`, then explicitly refer to their columns using `x$` # and `y$`. by <- join_by(chromosome, between(y$start, x$start, x$end)) full_join(reference, segments, by) ``` -------------------------------- ### Source for database backends (Deprecated) Source: https://cran.r-project.org/web/packages/dplyr/refman/dplyr.html/index Deprecated functions for creating database sources. Users should now use `tbl()` directly on a `DBIConnection`. ```APIDOC ## Source for database backends ### Description These functions have been deprecated; instead please use `tbl()` directly on an `DBIConnection`. See https://dbplyr.tidyverse.org/ for more details. ### Usage ```R src_mysql( dbname, host = NULL, port = 0L, username = "root", password = "", ... ) src_postgres( dbname = NULL, host = NULL, port = NULL, user = NULL, password = NULL, ... ) src_sqlite(path, create = FALSE) ``` ### Arguments `dbname` | Database name `host`, `port` | Host name and port number of database `...` | for the src, other arguments passed on to the underlying database connector, `DBI::dbConnect()`. For the tbl, included for compatibility with the generic, but otherwise ignored. `user`, `username`, `password` | User name and password. Generally, you should avoid saving username and password in your scripts as it is easy to accidentally expose valuable credentials. Instead, retrieve them from environment variables, or use database specific credential scores. For example, with MySQL you can set up `my.cnf` as described in `RMySQL::MySQL()`. `path` | Path to SQLite database. You can use the special path ":memory:" to create a temporary in memory database. `create` | if `FALSE`, `path` must already exist. If `TRUE`, will create a new SQLite3 database at `path` if `path` does not exist and connect to the existing database if `path` does exist. ### Value An S3 object with class `src_dbi`, `src_sql`, `src`. ### Examples ```R con <- DBI::dbConnect(RSQLite::SQLite(), ":memory:") copy_to(con, mtcars) # To retrieve a single table from a source, use `tbl()` mtcars <- con %>% tbl("mtcars") mtcars # You can also use pass raw SQL if you want a more sophisticated query con %>% tbl(sql("SELECT * FROM mtcars WHERE cyl == 8")) ``` ``` -------------------------------- ### Range-Based Closest Match Join with dplyr Source: https://cran.r-project.org/web/packages/dplyr/refman/dplyr.html/index Combines a closest match condition with a range condition. It finds the closest promo date that occurred within one day before the sale date. This example uses `full_join` to show all 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) ``` -------------------------------- ### Mutate: Controlling New Column Placement (R) Source: https://cran.r-project.org/web/packages/dplyr/refman/dplyr.html/index Illustrates how to control the position of newly created columns using the .before and .after arguments within mutate(). This allows for precise arrangement of columns in the output data frame. ```r df <- tibble(x = 1, y = 2) df %> mutate(z = x + y) df %> mutate(z = x + y, .before = 1) df %> mutate(z = x + y, .after = x) ``` -------------------------------- ### Summarize data by month in R using dplyr Source: https://cran.r-project.org/web/packages/dplyr/refman/dplyr.html/index This code snippet demonstrates how to group a data frame by the 'month' column and calculate the average 'temp' for each month using dplyr's group_by() and summarise() functions. It also shows an example of the output format. ```r df %> group_by(month) %> summarise(average_temp = mean(temp)) ``` -------------------------------- ### Summarise columns using summarise_at and across in R Source: https://cran.r-project.org/web/packages/dplyr/refman/dplyr.html/index Demonstrates how to use summarise_at with strings and vars() for column selection, and their equivalent using the across() function. It also shows how to apply summarise_if with a predicate function and its across() equivalent. ```R starwars %> summarise_at(c("height", "mass"), mean, na.rm = TRUE) # -> starwars %> summarise(across(c("height", "mass"), ~ mean(.x, na.rm = TRUE))) starwars %> summarise_at(vars(height:mass), mean, na.rm = TRUE) # -> starwars %> summarise(across(height:mass, ~ mean(.x, na.rm = TRUE))) starwars %> summarise_if(is.numeric, mean, na.rm = TRUE) starwars %> summarise(across(where(is.numeric), ~ mean(.x, na.rm = TRUE))) ``` -------------------------------- ### List all tbls provided by a source Source: https://cran.r-project.org/web/packages/dplyr/refman/dplyr.html/index A generic method for listing all tables (`tbls`) provided by a data source. Individual `src` objects will provide specific methods. ```APIDOC ## List all tbls provided by a source. ### Description This is a generic method which individual src's will provide methods for. Most methods will not be documented because it's usually pretty obvious what possible results will be. ### Usage ```R src_tbls(x, ...) ``` ### Arguments `x` | a data src. `...` | other arguments passed on to the individual methods. ``` -------------------------------- ### Selecting Rows by Min/Max Values with slice_min() and slice_max() Source: https://cran.r-project.org/web/packages/dplyr/refman/dplyr.html/index Shows how to use slice_min() and slice_max() to select rows based on the minimum or maximum values of a specified variable. It also covers handling ties using the 'with_ties' argument and breaking ties with additional variables. ```r mtcars %>% slice_min(mpg, n = 5) mtcars %>% slice_max(mpg, n = 5) mtcars %>% slice_min(cyl, n = 1) mtcars %>% slice_min(cyl, n = 1, with_ties = FALSE) mtcars %>% slice_min(tibble(cyl, mpg), n = 1) ``` -------------------------------- ### Get Current Group Information (dplyr) Source: https://cran.r-project.org/web/packages/dplyr/refman/dplyr.html/index These R functions retrieve information about the current group or column within specific dplyr contexts like summarise() and mutate(). They are useful for understanding group size, keys, identifiers, row indices, and column names during data manipulation. They do not work outside these specific contexts. ```r df <- tibble( g = sample(rep(letters[1:3], 1:3)), x = runif(6), y = runif(6) ) gf <- df %>% group_by(g) gf %>% summarise(n = n()) gf %>% mutate(id = cur_group_id()) gf %>% reframe(row = cur_group_rows()) gf %>% summarise(data = list(cur_group())) gf %>% mutate(across(everything(), ~ paste(cur_column(), round(.x, 2)))) ``` -------------------------------- ### Apply multiple transformations to all columns using summarise_all and across in R Source: https://cran.r-project.org/web/packages/dplyr/refman/dplyr.html/index Illustrates how to apply multiple transformations to all columns using summarise_all with a list of functions, and its equivalent using the across() function with named functions. ```R by_species %> summarise_all(list(min, max)) # -> by_species %> summarise(across(everything(), list(min = min, max = max))) ``` -------------------------------- ### Create a table from a data source using tbl in R Source: https://cran.r-project.org/web/packages/dplyr/refman/dplyr.html/index Demonstrates the usage of the tbl() function to create a table from a data source and is.tbl() to check if an object is a table. ```R tbl(src, ...) is.tbl(x) ``` -------------------------------- ### Chaining group_by and summarise operations Source: https://cran.r-project.org/web/packages/dplyr/refman/dplyr.html/index Illustrates how multiple grouping and summarising steps can be chained together. Each summarise call can remove a layer of grouping, allowing for hierarchical aggregation. ```R by_vs_am <- mtcars %>% group_by(vs, am) by_vs <- by_vs_am %>% summarise(n = n()) by_vs by_vs %>% summarise(n = sum(n)) ``` -------------------------------- ### Grouping by expressions Source: https://cran.r-project.org/web/packages/dplyr/refman/dplyr.html/index Illustrates how to group by the result of an expression. This acts as a shorthand for a mutate() step followed by a group_by() step, performed on the ungrouped data. ```R # The implicit mutate() step is always performed on the # ungrouped data. Here we get 3 groups: mtcars %>% group_by(vs) %>% group_by(hp_cut = cut(hp, 3)) ``` -------------------------------- ### Summarise using .data pronoun Source: https://cran.r-project.org/web/packages/dplyr/refman/dplyr.html/index Shows how to refer to column names stored as strings using the `.data` pronoun within summarise. This is useful for dynamic column selection and adheres to tidy evaluation principles. ```R var <- "mass" summarise(starwars, avg = mean(.data[[var]], na.rm = TRUE)) ``` -------------------------------- ### Create Source Objects (src) in R Source: https://cran.r-project.org/web/packages/dplyr/refman/dplyr.html/index `src()` is the constructor for source objects, and `is.src()` tests for their existence. When creating a `src` object, you must supply the subclass name, as 'src' is an abstract base class. Additional fields can be provided via splicing. ```R src(subclass, ...) is.src(x) ``` -------------------------------- ### Grouping with factor levels and .drop = FALSE Source: https://cran.r-project.org/web/packages/dplyr/refman/dplyr.html/index Demonstrates how to group by a factor variable while preserving all factor levels, even if they don't appear in the data, by setting .drop = FALSE. This ensures all potential groups are represented. ```R tbl <- tibble( x = 1:10, y = factor(rep(c("a", "c"), each = 5), levels = c("a", "b", "c")) ) tbl %>% group_by(y, .drop = FALSE) %>% group_rows() ``` -------------------------------- ### Database and SQL Generics (R) Source: https://cran.r-project.org/web/packages/dplyr/refman/dplyr.html/index Details the `sql_` generics used for building SQL queries and `db_` generics for executing database actions in R. It covers functions for translating environments, listing tables, checking table existence, data types, saving queries, transaction management, table manipulation, and query explanation. ```R db_desc(x) sql_translate_env(con) db_list_tables(con) db_has_table(con, table) db_data_type(con, fields) db_save_query(con, sql, name, temporary = TRUE, ...) db_begin(con, ...) db_commit(con, ...) db_rollback(con, ...) db_write_table(con, table, types, values, temporary = FALSE, ...) db_create_table(con, table, types, temporary = FALSE, ...) db_insert_into(con, table, values, ...) db_create_indexes(con, table, indexes = NULL, unique = FALSE, ...) db_create_index(con, table, columns, name = NULL, unique = FALSE, ...) db_drop_table(con, table, force = FALSE, ...) db_analyze(con, table, ...) db_explain(con, sql, ...) db_query_fields(con, sql, ...) db_query_rows(con, sql, ...) sql_select( con, select, from, where = NULL, group_by = NULL, having = NULL, order_by = NULL, limit = NULL, distinct = FALSE, ... ) sql_subquery(con, from, name = random_table_name(), ...) sql_join(con, x, y, vars, type = "inner", by = NULL, ...) sql_semi_join(con, x, y, anti = FALSE, by = NULL, ...) sql_set_op(con, x, y, method) sql_escape_string(con, x) sql_escape_ident(con, x) ``` -------------------------------- ### Mutate: Using across() for Multiple Columns (R) Source: https://cran.r-project.org/web/packages/dplyr/refman/dplyr.html/index Shows how to apply a transformation to multiple columns simultaneously using mutate() in conjunction with the across() function. This is useful for consistent data type conversions or applying the same operation across a subset of columns. ```r starwars %> select(name, homeworld, species) %> mutate(across(!name, as.factor)) ``` -------------------------------- ### Create a Tibble Object Source: https://cran.r-project.org/web/packages/dplyr/refman/dplyr.html/index The standard constructor for tibbles. `as.tbl()` is used for coercion, and `is.tbl()` for testing. `make_tbl` requires a subclass name and can accept additional fields. ```r make_tbl(subclass, ...) ``` -------------------------------- ### Handle packed data frames from custom functions with reframe() Source: https://cran.r-project.org/web/packages/dplyr/refman/dplyr.html/index Illustrates how to use the reframe() function with across() to apply custom functions that return data frames. It shows how to manage 'packed' data frames and how to use .unpack to automatically expand them into individual columns, optionally with custom naming. ```r quantile_df <- function(x, probs = c(0.25, 0.5, 0.75)) { tibble(quantile = probs, value = quantile(x, probs)) } iris %> reframe(across(starts_with("Sepal"), quantile_df)) iris %> reframe(across(starts_with("Sepal"), quantile_df, .unpack = TRUE)) iris %> reframe(across(starts_with("Sepal"), quantile_df, .unpack = "{outer}.{inner}")) ``` -------------------------------- ### Apply functions to columns matching a pattern or condition Source: https://cran.r-project.org/web/packages/dplyr/refman/dplyr.html/index Demonstrates how to apply functions to columns in a data frame using `mutate_at` and `across`. These functions allow for selective column transformation based on names or data types. ```R iris %>% mutate_at(vars(matches("Sepal")), log) iris %>% mutate(across(matches("Sepal"), log)) ``` ```R starwars %>% mutate_if(is.numeric, scale2, na.rm = TRUE) starwars %>% mutate(across(where(is.numeric), ~ scale2(.x, na.rm = TRUE))) ``` ```R iris %>% mutate_if(is.factor, as.character) iris %>% mutate_if(is.double, as.integer) # -> iris %>% mutate(across(where(is.factor), as.character)) iris %>% mutate(across(where(is.double), as.integer)) ``` ```R iris %>% mutate_if(is.numeric, list(scale2, log)) iris %>% mutate_if(is.numeric, list(~scale2(.), ~log(.))) iris %>% mutate_if(is.numeric, list(scale = scale2, log = log)) # -> iris %>% as_tibble() %>% mutate(across(where(is.numeric), list(scale = scale2, log = log))) ``` ```R iris %>% mutate_if(is.numeric, list(scale2)) iris %>% mutate_if(is.numeric, list(scale = scale2)) ``` -------------------------------- ### Select and Rename Columns in R using dplyr Source: https://cran.r-project.org/web/packages/dplyr/refman/dplyr.html/index Demonstrates selecting columns based on conditions (like data type or name patterns) and renaming them to uppercase using dplyr functions in R. It shows how to use `select()` with `where()` and `rename_with()` for flexible column manipulation. ```r mtcars %>% select(where(is_whole)) %>% rename_with(toupper) ``` ```r mtcars %>% select_at(vars(-contains("ar"), starts_with("c")), toupper) ``` ```r mtcars %>% select(!contains("ar") | starts_with("c")) %>% rename_with(toupper) ``` -------------------------------- ### Apply multi-lag helper with across() and .unpack Source: https://cran.r-project.org/web/packages/dplyr/refman/dplyr.html/index Shows how to use a custom multi-lag helper function within mutate() combined with across() and .unpack. This allows for the creation of multiple lagged columns from a single operation, automatically expanding the results. ```r multilag <- function(x, lags = 1:3) { names(lags) <- as.character(lags) purrr::map_dfr(lags, lag, x = x) } iris %> group_by(Species) %> mutate(across(starts_with("Sepal"), multilag, .unpack = TRUE)) %> select(Species, starts_with("Sepal")) ``` -------------------------------- ### Handle default values with case_match() and NA Source: https://cran.r-project.org/web/packages/dplyr/refman/dplyr.html/index Shows how to use case_match() to replace values in a character vector, similar to recode(), but using NA as the default. This demonstrates case_match()'s ability to handle different types for default values more flexibly. ```R char_vec <- sample(c("a", "b", "c"), 10, replace = TRUE) case_match(char_vec, "a" ~ "Apple", "b" ~ "Banana", .default = NA) ``` -------------------------------- ### Conditional filtering with if_any() and if_all() Source: https://cran.r-project.org/web/packages/dplyr/refman/dplyr.html/index Demonstrates the use of if_any() and if_all() for filtering data frames based on conditions applied to subsets of columns. if_any() filters rows where at least one column meets the condition, while if_all() filters rows where all specified columns meet the condition. ```r iris %> filter(if_any(ends_with("Width"), ~ . > 4)) iris %> filter(if_all(ends_with("Width"), ~ . > 2)) ``` -------------------------------- ### distinct_prepare and group_by_prepare Source: https://cran.r-project.org/web/packages/dplyr/refman/dplyr.html/index These functions perform standard manipulations needed prior to actual data processing. They are primarily used by packages implementing dplyr backends. ```APIDOC ## distinct_prepare and group_by_prepare ### Description `*_prepare()` performs standard manipulation that is needed prior to actual data processing. They are only be needed by packages that implement dplyr backends. ### Usage ```R distinct_prepare( .data, vars, group_vars = character(), .keep_all = FALSE, caller_env = caller_env(2), error_call = caller_env() ) group_by_prepare( .data, ..., .add = FALSE, .dots = deprecated(), add = deprecated(), error_call = caller_env() ) ``` ### Value A list `data` | Modified tbl ---|--- `groups` | Modified groups ``` -------------------------------- ### Summarise and group variables Source: https://cran.r-project.org/web/packages/dplyr/refman/dplyr.html/index Demonstrates how each summarise call reduces the grouping level. It shows the effect of summarise on grouping variables and how to inspect the remaining group variables. ```R mtcars %> group_by(cyl, vs) %> summarise(cyl_n = n()) %> group_vars() ``` -------------------------------- ### Vectorized switch statement with multiple conditions Source: https://cran.r-project.org/web/packages/dplyr/refman/dplyr.html/index Provides a vectorized alternative to `switch()`, allowing multiple conditions to be evaluated sequentially. It maps input values to output values based on matching conditions, with a `.default` for unmatched cases. Similar to SQL's `CASE WHEN`. ```r x <- c("a", "b", "a", "d", "b", NA, "c", "e") # `case_match()` acts like a vectorized `switch()`. # Unmatched values "fall through" as a missing value. case_match( x, "a" ~ 1, "b" ~ 2, "c" ~ 3, "d" ~ 4 ) # Missing values can be matched exactly, and `.default` can be used to # control the value used for unmatched values of `.x` case_match( x, "a" ~ 1, "b" ~ 2, "c" ~ 3, "d" ~ 4, NA ~ 0, .default = 100 ) # Input values can be grouped into the same expression to map them to the # same output value case_match( x, c("a", "b") ~ "low", c("c", "d", "e") ~ "high" ) # `case_match()` isn't limited to character input: y <- c(1, 2, 1, 3, 1, NA, 2, 4) case_match( y, c(1, 3) ~ "odd", c(2, 4) ~ "even", .default = "missing" ) # Setting `.default` to the original vector is a useful way to replace # selected values, leaving everything else as is case_match(y, NA ~ 0, .default = y) starwars %>% mutate( # Replace missings, but leave everything else alone hair_color = case_match(hair_color, NA ~ "unknown", .default = hair_color), # Replace some, but not all, of the species species = case_match( species, "Human" ~ "Humanoid", "Droid" ~ "Robot", c("Wookiee", "Ewok") ~ "Hairy", .default = species ), .keep = "used" ) ``` -------------------------------- ### Handle default values with recode() and NA_character_ Source: https://cran.r-project.org/web/packages/dplyr/refman/dplyr.html/index Illustrates using recode() with a character vector, specifying replacements for 'a' and 'b', and using NA_character_ as the default value for unmatched elements. This highlights type compatibility. ```R char_vec <- sample(c("a", "b", "c"), 10, replace = TRUE) recode(char_vec, a = "Apple", b = "Banana", .default = NA_character_) ``` -------------------------------- ### Replace values in character vector with case_match() Source: https://cran.r-project.org/web/packages/dplyr/refman/dplyr.html/index Demonstrates replacing specific character values ('a', 'b') in a character vector with new values ('Apple', 'Banana') using the case_match() function, which is the recommended replacement for recode(). It also shows how to handle default values. ```R char_vec <- sample(c("a", "b", "c"), 10, replace = TRUE) case_match(char_vec, "a" ~ "Apple", "b" ~ "Banana", .default = char_vec) ``` -------------------------------- ### Select multiple variables by name using dplyr::select Source: https://cran.r-project.org/web/packages/dplyr/refman/dplyr.html/index Shows how to select multiple columns ('homeworld', 'height', 'mass') from the 'starwars' dataset using `dplyr::select`. The order of the output columns matches the order of specification. ```r starwars %>% select(homeworld, height, mass) ``` -------------------------------- ### Database Backend Source Functions (Deprecated) in R Source: https://cran.r-project.org/web/packages/dplyr/refman/dplyr.html/index These functions (`src_mysql`, `src_postgres`, `src_sqlite`) were used to create source objects for different database backends. They are now deprecated in favor of using `tbl()` directly on a `DBIConnection`. The arguments specify database connection details and options for creating SQLite databases. ```R src_mysql( dbname, host = NULL, port = 0L, username = "root", password = "", ... ) src_postgres( dbname = NULL, host = NULL, port = NULL, user = NULL, password = NULL, ... ) src_sqlite(path, create = FALSE) ``` -------------------------------- ### Overriding and adding to existing groups Source: https://cran.r-project.org/web/packages/dplyr/refman/dplyr.html/index Demonstrates the behavior of group_by() with respect to existing groups. By default, it overrides existing groups, but the .add = TRUE argument allows appending new grouping variables. ```R # By default, group_by() overrides existing grouping by_cyl %>% group_by(vs, am) %>% group_vars() # Use add = TRUE to instead append by_cyl %>% group_by(vs, am, .add = TRUE) %>% group_vars() ```