### R: Initialize data.table for examples Source: https://github.com/markfairbanks/tidytable/blob/main/docs/reference/slice_tail..html Initializes a sample `data.table` named `df` with columns `x`, `y`, and `z` to be used in subsequent `tidytable` examples. ```R df <- data.table( x = 1:4, y = 5:8, z = c("a", "a", "a", "b") ) ``` -------------------------------- ### Install tidytable R Package Source: https://github.com/markfairbanks/tidytable/blob/main/README.md Instructions for installing the tidytable R package from CRAN (the released version) or the development version directly from GitHub using the pak package. ```R install.packages("tidytable") ``` ```R # install.packages("pak") pak::pak("markfairbanks/tidytable") ``` -------------------------------- ### R tidytable Select Columns Starting with a Prefix Source: https://github.com/markfairbanks/tidytable/blob/main/docs/reference/starts_with..html Demonstrates how to use `starts_with` within `select.` to filter columns in a `tidytable` based on a name prefix. This example initializes a `tidytable` and then selects columns whose names begin with 'x'. ```R test_df <- tidytable( x = 1, y = 2, double_x = 2, double_y = 4) test_df %>% select.(starts_with("x")) ``` -------------------------------- ### R tidytable nest_by Usage Examples Source: https://github.com/markfairbanks/tidytable/blob/main/docs/reference/nest_by.html Practical examples demonstrating how to use `nest_by` with a sample `data.table`. Examples include nesting the entire table, nesting by specific columns, using `where(is.character)` for column selection, and controlling the `.keep` argument. ```R df <- data.table( a = 1:5, b = 6:10, c = c(rep("a", 3), rep("b", 2)), d = c(rep("a", 3), rep("b", 2)) ) df %>% nest_by() #> # A tidytable: 1 × 1 #> data #> #> 1 df %>% nest_by(c, d) #> # A tidytable: 2 × 3 #> c d data #> #> 1 a a #> 2 b b df %>% nest_by(where(is.character)) #> # A tidytable: 2 × 3 #> c d data #> #> 1 a a #> 2 b b df %>% nest_by(c, d, .keep = TRUE) #> # A tidytable: 2 × 3 #> c d data #> #> 1 a a #> 2 b b ``` -------------------------------- ### R tidytable expand_grid Basic Example Source: https://github.com/markfairbanks/tidytable/blob/main/docs/reference/expand_grid..html An example demonstrating the basic usage of `expand_grid` to create a data.table of all combinations from two simple integer sequences `x` and `y`. ```R x <- 1:2 y <- 1:2 expand_grid(x, y) ``` -------------------------------- ### R tidytable::arrange. Examples for Reordering Data Source: https://github.com/markfairbanks/tidytable/blob/main/docs/reference/arrange..html Illustrates practical usage of the `tidytable::arrange.` function with R code examples. It demonstrates how to initialize a data.table and then reorder its rows based on one or more columns, including examples of descending order using both the unary minus operator and the `desc()` helper function. ```R df <- data.table( a = 1:3, b = 4:6, c = c("a", "a", "b") ) df %>% arrange(c, -a) #> # A tidytable: 3 × 3 #> a b c #> #> 1 2 5 a #> 2 1 4 a #> 3 3 6 b ``` ```R df %>% arrange(c, desc(a)) #> # A tidytable: 3 × 3 #> a b c #> #> 1 2 5 a #> 2 1 4 a #> 3 3 6 b ``` -------------------------------- ### Example Usage of tidytable::fread() Source: https://github.com/markfairbanks/tidytable/blob/main/docs/reference/fread.html This example demonstrates how to use `fread()` to read data from a string, showing the input CSV content and the resulting tidytable output. ```R fake_csv <- "A,B\n 1,2\n 3,4" fread(fake_csv) #> # A tidytable: 2 × 2 #> A B #> #> 1 1 2 #> 2 3 4 ``` -------------------------------- ### R Package Installation Failure Summary Source: https://github.com/markfairbanks/tidytable/blob/main/revdep/failures.md This snippet shows the general error message indicating that an R package installation failed and directs the user to a log file for more details. It's a common output when R package compilation or installation encounters issues. ```Shell Installation failed. See ‘/Users/xmxf129/MyDocs/R/Packages/tidytable/revdep/checks.noindex/bbknnR/new/bbknnR.Rcheck/00install.out’ for details. ``` ```Shell Installation failed. See ‘/Users/xmxf129/MyDocs/R/Packages/tidytable/revdep/checks.noindex/rTwig/new/rTwig.Rcheck/00install.out’ for details. ``` -------------------------------- ### R tidytable expand and nesting Function Examples Source: https://github.com/markfairbanks/tidytable/blob/main/docs/reference/nesting..html Examples demonstrating the usage of `expand()` to generate all possible combinations of specified columns and `nesting()` to generate combinations already present in the data. The examples show the input data.table and the resulting expanded data.table. ```R df <- tidytable(x = c(1, 1, 2), y = c(1, 1, 2)) df %>% expand(x, y) #> # A tidytable: 4 × 2 #> x y #> #> 1 1 1 #> 2 1 2 #> 3 2 1 #> 4 2 2 df %>% expand(nesting(x, y)) #> # A tidytable: 2 × 2 #> x y #> #> 1 1 1 #> 2 2 2 ``` -------------------------------- ### R tidytable lag and lead Usage Examples Source: https://github.com/markfairbanks/tidytable/blob/main/docs/reference/leads..html Illustrative examples demonstrating how to use `lag` and `lead` functions in R, both independently and within a `mutate()` operation on a `tidytable` dataframe to create new columns based on shifted values. ```R x <- 1:5 lag(x, 1) #> [1] NA 1 2 3 4 lead(x, 1) #> [1] 2 3 4 5 NA # Also works inside of `mutate()` df <- tidytable(x = 1:5) df %>% mutate(lag_x = lag(x)) #> # A tidytable: 5 × 2 #> x lag_x #> #> 1 1 NA #> 2 2 1 #> 3 3 2 #> 4 4 3 #> 5 5 4 ``` -------------------------------- ### R: Example of Using group_vars. with tidytable Source: https://github.com/markfairbanks/tidytable/blob/main/docs/reference/group_vars..html Illustrates the practical application of `group_vars.` in R. The example creates a `data.table`, groups it by columns `c` and `d`, and then uses `group_vars.` to confirm the active grouping variables. ```R df <- data.table( a = 1:3, b = 4:6, c = c("a", "a", "b"), d = c("a", "a", "b") ) df %>% group_by(c, d) %>% group_vars() #> [1] "c" "d" ``` -------------------------------- ### Example: Creating a tidytable from an R list Source: https://github.com/markfairbanks/tidytable/blob/main/docs/reference/new_tidytable.html Demonstrates how to use the `new_tidytable` function in R to convert a named list into a tidytable. The example shows the input list structure and the resulting tidytable output. ```R l <- list(x = 1:3, y = c("a", "a", "b")) new_tidytable(l) #> # A tidytable: 3 × 2 #> x y #> #> 1 1 a #> 2 2 a #> 3 3 b ``` -------------------------------- ### R `expand_grid` Usage Examples Source: https://github.com/markfairbanks/tidytable/blob/main/docs/reference/expand_grid.html Provides practical examples of using the `expand_grid` function in R. It demonstrates how to generate combinations from simple numeric vectors, both with default naming and when explicitly naming input variables, showing the resulting `tidytable` output. ```R x <- 1:2 y <- 1:2 expand_grid(x, y) #> # A tidytable: 4 × 2 #> x y #> #> 1 1 1 #> 2 1 2 #> 3 2 1 #> 4 2 2 expand_grid(stuff = x, y) #> # A tidytable: 4 × 2 #> stuff y #> #> 1 1 1 #> 2 1 2 #> 3 2 1 #> 4 2 2 ``` -------------------------------- ### R Examples: Joining data.tables with tidytable Source: https://github.com/markfairbanks/tidytable/blob/main/docs/reference/left_join..html Practical R examples demonstrating how to use `left_join`, `inner_join`, `right_join`, `full_join`, and `anti_join` from the `tidytable` package. It shows how to initialize data.tables and apply different join types to combine them, illustrating the resulting data structures. ```R df1 <- data.table(x = c("a", "a", "b", "c"), y = 1:4) df2 <- data.table(x = c("a", "b"), z = 5:6) df1 %>% left_join(df2) #> # A tidytable: 4 × 3 #> x y z #> #> 1 a 1 5 #> 2 a 2 5 #> 3 b 3 6 #> 4 c 4 NA df1 %>% inner_join(df2) #> # A tidytable: 3 × 3 #> x y z #> #> 1 a 1 5 #> 2 a 2 5 #> 3 b 3 6 df1 %>% right_join(df2) #> # A tidytable: 3 × 3 #> x y z #> #> 1 a 1 5 #> 2 a 2 5 #> 3 b 3 6 df1 %>% full_join(df2) #> # A tidytable: 4 × 3 #> x y z #> #> 1 a 1 5 #> 2 a 2 5 #> 3 b 3 6 #> 4 c 4 NA df1 %>% anti_join(df2) #> # A tidytable: 1 × 2 #> x y #> #> 1 c 4 ``` -------------------------------- ### R `lag` and `lead` Function Usage Examples Source: https://github.com/markfairbanks/tidytable/blob/main/docs/reference/lag.html Practical examples demonstrating the application of `lag` and `lead` functions in R. Includes basic vector manipulation and integration with `tidytable`'s `mutate()` for data frame operations. ```R x <- 1:5 lag(x, 1) #> [1] NA 1 2 3 4 lead(x, 1) #> [1] 2 3 4 5 NA # Also works inside of `mutate()` df <- tidytable(x = 1:5) df %>% mutate(lag_x = lag(x)) #> # A tidytable: 5 × 2 #> x lag_x #> #> 1 1 NA #> 2 2 1 #> 3 3 2 #> 4 4 3 #> 5 5 4 ``` -------------------------------- ### Examples of unite. Function Usage in R Source: https://github.com/markfairbanks/tidytable/blob/main/docs/reference/unite..html Practical examples demonstrating how to use the `unite.` function with various parameters and column selections, including basic concatenation, `tidyselect` integration, and handling of `remove` and `na.rm` options. ```R df <- tidytable( a = c("a", "a", "a"), b = c("b", "b", "b"), c = c("c", "c", NA) ) df %>% unite("new_col", b, c) ``` ```R df %>% unite("new_col", where(is.character)) ``` ```R df %>% unite("new_col", b, c, remove = FALSE) ``` ```R df %>% unite("new_col", b, c, na.rm = TRUE) ``` ```R df %>% unite() ``` -------------------------------- ### Install tidytable R Package from CRAN or GitHub Source: https://github.com/markfairbanks/tidytable/blob/main/docs/index.html Install the tidytable package in R. Choose between the stable version from CRAN using `install.packages()` or the development version from GitHub using `pak::pak()` for the latest features. ```R install.packages("tidytable") ``` ```R # install.packages("pak") pak::pak("markfairbanks/tidytable") ``` -------------------------------- ### Examples of Nesting Columns with `nest.` in R Source: https://github.com/markfairbanks/tidytable/blob/main/docs/reference/nest..html Practical R code examples demonstrating various ways to use the `nest.` function from `tidytable` to group columns into a list-column, including selecting columns by name, by type, and grouping by other columns. ```R df <- data.table( a = 1:3, b = 1:3, c = c("a", "a", "b"), d = c("a", "a", "b") ) df %>% nest(data = c(a, b)) #> # A tidytable: 2 × 3 #> c d data #> #> 1 a a #> 2 b b ``` ```R df %>% nest(data = where(is.numeric)) #> # A tidytable: 2 × 3 #> c d data #> #> 1 a a #> 2 b b ``` ```R df %>% nest(.by = c(c, d)) #> # A tidytable: 2 × 3 #> c d data #> #> 1 a a #> 2 b b ``` -------------------------------- ### R Examples for tidytable::unite Function Source: https://github.com/markfairbanks/tidytable/blob/main/docs/reference/unite.html Illustrative R code examples demonstrating how to use the `unite` function from the `tidytable` package to combine columns with different options, including basic usage, `tidyselect` integration, column removal control, and NA handling. ```R df <- tidytable( a = c("a", "a", "a"), b = c("b", "b", "b"), c = c("c", "c", NA) ) df %>% unite("new_col", b, c) ``` ```R df %>% unite("new_col", where(is.character)) ``` ```R df %>% unite("new_col", b, c, remove = FALSE) ``` ```R df %>% unite("new_col", b, c, na.rm = TRUE) ``` ```R df %>% unite() ``` -------------------------------- ### R Package C++ Compilation Error: Missing cmath Header (bbknnR) Source: https://github.com/markfairbanks/tidytable/blob/main/revdep/failures.md This snippet details a compilation failure for the 'bbknnR' R package, specifically an error during C++ compilation where the 'cmath' header file could not be found. This typically indicates an issue with the C++ compiler setup or include paths. ```Shell * installing *source* package ‘bbknnR’ ... ** package ‘bbknnR’ successfully unpacked and MD5 sums checked ** using staged installation ** libs using C++ compiler: ‘Apple clang version 16.0.0 (clang-1600.0.26.4)’ using SDK: ‘MacOSX15.1.sdk’ clang++ -arch arm64 -std=gnu++17 -I"/Library/Frameworks/R.framework/Resources/include" -DNDEBUG -I'/Users/xmxf129/MyDocs/R/Packages/tidytable/revdep/library.noindex/bbknnR/Rcpp/include' -I/opt/R/arm64/include -I/opt/homebrew/opt/libomp/include -Xclang -fopenmp -fPIC -falign-functions=64 -Wall -g -O2 -c RcppExports.cpp -o RcppExports.o In file included from RcppExports.cpp:4: In file included from /Users/xmxf129/MyDocs/R/Packages/tidytable/revdep/library.noindex/bbknnR/Rcpp/include/Rcpp.h:27: In file included from /Users/xmxf129/MyDocs/R/Packages/tidytable/revdep/library.noindex/bbknnR/Rcpp/include/RcppCommon.h:30: In file included from /Users/xmxf129/MyDocs/R/Packages/tidytable/revdep/library.noindex/bbknnR/Rcpp/include/Rcpp/r/headers.h:66: /Users/xmxf129/MyDocs/R/Packages/tidytable/revdep/library.noindex/bbknnR/Rcpp/include/Rcpp/platform/compiler.h:100:10: fatal error: 'cmath' file not found 100 | #include | ^~~~~~~ 1 error generated. make: *** [RcppExports.o] Error 1 ERROR: compilation failed for package ‘bbknnR’ * removing ‘/Users/xmxf129/MyDocs/R/Packages/tidytable/revdep/checks.noindex/bbknnR/new/bbknnR.Rcheck/bbknnR’ ``` ```Shell * installing *source* package ‘bbknnR’ ... ** package ‘bbknnR’ successfully unpacked and MD5 sums checked ** using staged installation ** libs using C++ compiler: ‘Apple clang version 16.0.0 (clang-1600.0.26.4)’ using SDK: ‘MacOSX15.1.sdk’ clang++ -arch arm64 -std=gnu++17 -I"/Library/Frameworks/R.framework/Resources/include" -DNDEBUG -I'/Users/xmxf129/MyDocs/R/Packages/tidytable/revdep/library.noindex/bbknnR/Rcpp/include' -I/opt/R/arm64/include -I/opt/homebrew/opt/libomp/include -Xclang -fopenmp -fPIC -falign-functions=64 -Wall -g -O2 -c RcppExports.cpp -o RcppExports.o In file included from RcppExports.cpp:4: In file included from /Users/xmxf129/MyDocs/R/Packages/tidytable/revdep/library.noindex/bbknnR/Rcpp/include/Rcpp.h:27: In file included from /Users/xmxf129/MyDocs/R/Packages/tidytable/revdep/library.noindex/bbknnR/Rcpp/include/RcppCommon.h:30: In file included from /Users/xmxf129/MyDocs/R/Packages/tidytable/revdep/library.noindex/bbknnR/Rcpp/include/Rcpp/r/headers.h:66: /Users/xmxf129/MyDocs/R/Packages/tidytable/revdep/library.noindex/bbknnR/Rcpp/include/Rcpp/platform/compiler.h:100:10: fatal error: 'cmath' file not found 100 | #include | ^~~~~~~ 1 error generated. make: *** [RcppExports.o] Error 1 ERROR: compilation failed for package ‘bbknnR’ * removing ‘/Users/xmxf129/MyDocs/R/Packages/tidytable/revdep/checks.noindex/bbknnR/old/bbknnR.Rcheck/bbknnR’ ``` -------------------------------- ### R Examples for tidytable Row Slicing Source: https://github.com/markfairbanks/tidytable/blob/main/docs/reference/slice.html Demonstrates how to use `slice`, `slice_head`, `slice_tail`, `slice_max`, and `slice_min` functions from the `tidytable` package in R to select rows from a data.table, including examples with and without grouping by a column. ```R df <- data.table( x = 1:4, y = 5:8, z = c("a", "a", "a", "b") ) df %>% slice(1:3) df %>% slice(1, 3) df %>% slice(1:2, .by = z) df %>% slice_head(1, .by = z) df %>% slice_tail(1, .by = z) df %>% slice_max(order_by = x, .by = z) df %>% slice_min(order_by = y, .by = z) ``` -------------------------------- ### R Examples for tidytable's count and tally functions Source: https://github.com/markfairbanks/tidytable/blob/main/docs/reference/tally..html Practical R code examples demonstrating how to use `count()` and `tally()` from the `tidytable` package. Examples cover basic counting, grouping by specific columns, using `tidyselect` helpers, applying frequency weights, sorting results, and combining with `group_by()`. ```R df <- data.table( x = c("a", "a", "b"), y = c("a", "a", "b"), z = 1:3 ) ``` ```R df %>% count() ``` ```R df %>% count(x) ``` ```R df %>% count(where(is.character)) ``` ```R df %>% count(x, wt = z, name = "x_sum") ``` ```R df %>% count(x, sort = TRUE) ``` ```R df %>% tally() ``` ```R df %>% group_by(x) %>% tally() ``` -------------------------------- ### R Example: Coercing a data.frame to tidytable Source: https://github.com/markfairbanks/tidytable/blob/main/docs/reference/as_tidytable.html An R code example demonstrating the use of the `as_tidytable()` function to manually convert a standard `data.frame` into a `tidytable` object. The example shows the input `data.frame` and the resulting `tidytable` output. ```R df <- data.frame(x = -2:2, y = c(rep("a", 3), rep("b", 2))) df %>% as_tidytable() ``` -------------------------------- ### R tidytable summarize. Usage Examples Source: https://github.com/markfairbanks/tidytable/blob/main/docs/reference/summarize..html Illustrates practical usage of the `summarize.` function. The first example aggregates `avg_a` and `max_b` by column `c`. The second example aggregates `avg_a` by multiple columns `c` and `d`, demonstrating flexible grouping capabilities. ```R df <- data.table( a = 1:3, b = 4:6, c = c("a", "a", "b"), d = c("a", "a", "b") ) df %>% summarize(avg_a = mean(a), max_b = max(b), .by = c) #> # A tidytable: 2 × 3 #> c avg_a max_b #> #> 1 a 1.5 5 #> 2 b 3 6 df %>% summarize(avg_a = mean(a), .by = c(c, d)) #> # A tidytable: 2 × 3 #> c d avg_a #> #> 1 a a 1.5 #> 2 b b 3 ``` -------------------------------- ### R tidytable fill. Usage Examples Source: https://github.com/markfairbanks/tidytable/blob/main/docs/reference/fill..html Practical R examples demonstrating how to use the `fill.` function to handle missing values, including basic filling, filling by group, and filling with specific directions like 'downup'. ```R df <- data.table( a = c(1, NA, 3, 4, 5), b = c(NA, 2, NA, NA, 5), groups = c("a", "a", "a", "b", "b") ) df %>% fill(a, b) ``` ```R df <- data.table( a = c(1, NA, 3, 4, 5), b = c(NA, 2, NA, NA, 5), groups = c("a", "a", "a", "b", "b") ) df %>% fill(a, b, .by = groups) ``` ```R df <- data.table( a = c(1, NA, 3, 4, 5), b = c(NA, 2, NA, NA, 5), groups = c("a", "a", "a", "b", "b") ) df %>% fill(a, b, .direction = "downup", .by = groups) ``` -------------------------------- ### R tidytable group_split. Usage Examples Source: https://github.com/markfairbanks/tidytable/blob/main/docs/reference/group_split..html Illustrative examples demonstrating how to use `group_split.` with different parameters, including basic splitting, splitting without keeping grouping columns, and splitting with named output lists. ```R df <- tidytable( a = 1:3, b = 1:3, c = c("a", "a", "b"), d = c("a", "a", "b") ) df %>% group_split(c, d) ``` ```R df %>% group_split(c, d, .keep = FALSE) ``` ```R df %>% group_split(c, d, .named = TRUE) ``` -------------------------------- ### Examples of Data.table Join Operations in tidytable Source: https://github.com/markfairbanks/tidytable/blob/main/docs/reference/inner_join..html Illustrative R code examples demonstrating the usage of `left_join`, `inner_join`, `right_join`, `full_join`, and `anti_join` functions from the `tidytable` package to combine data.tables. ```R df1 <- data.table(x = c("a", "a", "b", "c"), y = 1:4) df2 <- data.table(x = c("a", "b"), z = 5:6) df1 %>% left_join(df2) #> # A tidytable: 4 × 3 #> x y z #> #> 1 a 1 5 #> 2 a 2 5 #> 3 b 3 6 #> 4 c 4 NA df1 %>% inner_join(df2) #> # A tidytable: 3 × 3 #> x y z #> #> 1 a 1 5 #> 2 a 2 5 #> 3 b 3 6 df1 %>% right_join(df2) #> # A tidytable: 3 × 3 #> x y z #> #> 1 a 1 5 #> 2 a 2 5 #> 3 b 3 6 df1 %>% full_join(df2) #> # A tidytable: 4 × 3 #> x y z #> #> 1 a 1 5 #> 2 a 2 5 #> 3 b 3 6 #> 4 c 4 NA df1 %>% anti_join(df2) #> # A tidytable: 1 × 2 #> x y #> #> 1 c 4 ``` -------------------------------- ### R tidytable `count()` and `tally()` Usage Examples Source: https://github.com/markfairbanks/tidytable/blob/main/docs/reference/count..html Illustrative R examples demonstrating various ways to use `count()` and `tally()` functions from the `tidytable` package, including basic counting, grouping by columns, using `tidyselect` helpers, applying weights, sorting results, and using `tally()` with `group_by()`. ```R df <- data.table( x = c("a", "a", "b"), y = c("a", "a", "b"), z = 1:3 ) df %>% count() ``` ```R df %>% count(x) ``` ```R df %>% count(where(is.character)) ``` ```R df %>% count(x, wt = z, name = "x_sum") ``` ```R df %>% count(x, sort = TRUE) ``` ```R df %>% tally() ``` ```R df %>% group_by(x) %>% tally() ``` -------------------------------- ### Examples of Row Selection with tidytable in R Source: https://github.com/markfairbanks/tidytable/blob/main/docs/reference/slice_head..html Demonstrates various row selection operations using `tidytable` functions like `slice`, `slice_head`, `slice_tail`, `slice_max`, and `slice_min`. Examples include selecting specific rows, ranges, and performing operations within groups. ```R df <- data.table( x = 1:4, y = 5:8, z = c("a", "a", "a", "b") ) df %>% slice(1:3) df %>% slice(1, 3) df %>% slice(1:2, .by = z) df %>% slice_head(1, .by = z) df %>% slice_tail(1, .by = z) df %>% slice_max(order_by = x, .by = z) df %>% slice_min(order_by = y, .by = z) ``` -------------------------------- ### R tidytable select. Column Selection Usage Examples Source: https://github.com/markfairbanks/tidytable/blob/main/docs/reference/select..html Practical R code examples demonstrating how to use the `select.` function to perform various column operations, including direct selection, range-based selection, dropping columns, pattern matching, conditional selection, and renaming columns. ```R df <- data.table( x1 = 1:3, x2 = 1:3, y = c("a", "b", "c"), z = c("a", "b", "c") ) df %>% select(x1, y) ``` ```R df <- data.table( x1 = 1:3, x2 = 1:3, y = c("a", "b", "c"), z = c("a", "b", "c") ) df %>% select(x1:y) ``` ```R df <- data.table( x1 = 1:3, x2 = 1:3, y = c("a", "b", "c"), z = c("a", "b", "c") ) df %>% select(-y, -z) ``` ```R df <- data.table( x1 = 1:3, x2 = 1:3, y = c("a", "b", "c"), z = c("a", "b", "c") ) df %>% select(starts_with("x"), z) ``` ```R df <- data.table( x1 = 1:3, x2 = 1:3, y = c("a", "b", "c"), z = c("a", "b", "c") ) df %>% select(where(is.character), x1) ``` ```R df <- data.table( x1 = 1:3, x2 = 1:3, y = c("a", "b", "c"), z = c("a", "b", "c") ) df %>% select(new = x1, y) ``` -------------------------------- ### R Examples: Applying Functions with tidytable map Variants Source: https://github.com/markfairbanks/tidytable/blob/main/docs/reference/map_df..html Practical R examples demonstrating how to use `map`, `map_dbl`, and `map_chr` from the `tidytable` package to apply a function to each element of a vector, showcasing different return types. ```R map(c(1,2,3), ~ .x + 1) ``` ```R map_dbl(c(1,2,3), ~ .x + 1) ``` ```R map_chr(c(1,2,3), as.character) ``` -------------------------------- ### R tidytable::nest() Usage Examples Source: https://github.com/markfairbanks/tidytable/blob/main/docs/reference/nest.html Demonstrates various ways to use the `nest` function in R with `tidytable`, including nesting specific columns, nesting all numeric columns, and nesting by grouping variables. Examples show how to create a data.table and apply `nest` with different column selection methods. ```R df <- data.table( a = 1:3, b = 1:3, c = c("a", "a", "b"), d = c("a", "a", "b") ) df %>% nest(data = c(a, b)) #> # A tidytable: 2 × 3 #> c d data #> #> 1 a a #> 2 b b df %>% nest(data = where(is.numeric)) #> # A tidytable: 2 × 3 #> c d data #> #> 1 a a #> 2 b b df %>% nest(.by = c(c, d)) #> # A tidytable: 2 × 3 #> c d data #> #> 1 a a #> 2 b b ``` -------------------------------- ### R Example: Combining data.tables row-wise with `bind_rows` Source: https://github.com/markfairbanks/tidytable/blob/main/docs/reference/bind_cols..html Demonstrates how to create and combine two `data.table` objects row-wise using the `bind_rows` function. Examples include direct binding with the pipe operator and binding from a list of data.tables. ```R df1 <- data.table(x = 1:3, y = 10:12) df2 <- data.table(x = 4:6, y = 13:15) df1 %>% bind_rows(df2) df_list <- list(df1, df2) bind_rows(df_list) ``` -------------------------------- ### R tidytable nest_by Usage Examples Source: https://github.com/markfairbanks/tidytable/blob/main/docs/reference/nest_by..html Examples demonstrating various ways to use the `nest_by` function in R's `tidytable` package, including nesting the entire data.table, nesting by specific columns, using `tidyselect` helpers, and controlling column retention with `.keep`. ```R df <- data.table( a = 1:5, b = 6:10, c = c(rep("a", 3), rep("b", 2)), d = c(rep("a", 3), rep("b", 2)) ) df %>% nest_by() #> # A tidytable: 1 × 1 #> data #> #> 1 df %>% nest_by(c, d) #> # A tidytable: 2 × 3 #> c d data #> #> 1 a a #> 2 b b df %>% nest_by(where(is.character)) #> # A tidytable: 2 × 3 #> c d data #> #> 1 a a #> 2 b b df %>% nest_by(c, d, .keep = TRUE) #> # A tidytable: 2 × 3 #> c d data #> #> 1 a a #> 2 b b ``` -------------------------------- ### R tidytable `case.` Example with `mutate` Source: https://github.com/markfairbanks/tidytable/blob/main/docs/reference/case..html An example demonstrating how to use `case.` within a `tidytable` workflow to create a new column based on multiple conditions and a default value. ```R df <- tidytable(x = 1:10) df %>% mutate(case_x = case(x < 5, 1, x < 7, 2, default = 3)) ``` -------------------------------- ### R tidytable: mutate_across. Usage Examples Source: https://github.com/markfairbanks/tidytable/blob/main/docs/reference/mutate_across..html Practical R code examples demonstrating various ways to use `mutate_across.`, including selecting numeric columns, specific columns, all columns, and applying multiple functions with custom naming. ```R df <- data.table( x = rep(1, 3), y = rep(2, 3), z = c("a", "a", "b") ) df %>% mutate_across.(where(is.numeric), as.character) df %>% mutate_across.(c(x, y), ~ .x * 2) df %>% mutate_across.(everything(), as.character) df %>% mutate_across.(c(x, y), list(new = ~ .x * 2, another = ~ .x + 7)) df %>% mutate_across.( .cols = c(x, y), .fns = list(new = ~ .x * 2, another = ~ .x + 7), .names = "{.col}_test_{.fn}" ) ``` -------------------------------- ### R tidytable Data Table Join Examples Source: https://github.com/markfairbanks/tidytable/blob/main/docs/reference/full_join..html Examples demonstrating how to perform various types of joins (`left_join`, `inner_join`, `right_join`, `full_join`, `anti_join`) using the `tidytable` package in R. It initializes two data.tables and applies different join operations. ```R df1 <- data.table(x = c("a", "a", "b", "c"), y = 1:4) df2 <- data.table(x = c("a", "b"), z = 5:6) df1 %>% left_join(df2) df1 %>% inner_join(df2) df1 %>% right_join(df2) df1 %>% full_join(df2) df1 %>% anti_join(df2) ``` -------------------------------- ### R tidytable expand Function Usage Examples Source: https://github.com/markfairbanks/tidytable/blob/main/docs/reference/expand..html Demonstrates how to use the `expand` function in `tidytable` to generate all combinations of specified columns from a data.table. Includes an example using `nesting()` to find only combinations already present in the dataset. ```R df <- tidytable(x = c(1, 1, 2), y = c(1, 1, 2)) df %>% expand(x, y) #> # A tidytable: 4 × 2 #> x y #> #> 1 1 1 #> 2 1 2 #> 3 2 1 #> 4 2 2 df %>% expand(nesting(x, y)) #> # A tidytable: 2 × 2 #> x y #> #> 1 1 1 #> 2 2 2 ``` -------------------------------- ### Get Group Row Indices using `cur_group_rows()` in R Source: https://github.com/markfairbanks/tidytable/blob/main/docs/reference/cur_data..html This example demonstrates `cur_group_rows()` within `tidytable`'s `mutate()` function. It provides the row indices relative to the start of each group, useful for operations that depend on the position within a group. ```R df <- data.table( x = 1:5, y = c("a", "a", "a", "b", "b") ) df %>% mutate(group_rows = cur_group_rows(), .by = y) ``` -------------------------------- ### Example: Constructing a tidytable Object Source: https://github.com/markfairbanks/tidytable/blob/main/docs/reference/tidytable.html Illustrates the practical application of the `tidytable` function to create a new tidytable object, demonstrating its input parameters and the resulting structured output. ```R tidytable(x = 1:3, y = c("a", "a", "b")) #> # A tidytable: 3 × 2 #> x y #> #> 1 1 a #> 2 2 a #> 3 3 b ``` -------------------------------- ### Example of tidytable::fread for Reading CSV Data Source: https://github.com/markfairbanks/tidytable/blob/main/docs/reference/fwrite..html This example demonstrates how to use `fread` from the `tidytable` package to read a simple CSV string. The function processes the input and returns a `tidytable` object, showcasing its wrapper functionality over `data.table::fread`. ```R fake_csv <- "A,B\n 1,2\n 3,4" fread(fake_csv) ``` -------------------------------- ### Example: Get Grouping Variables from tidytable in R Source: https://github.com/markfairbanks/tidytable/blob/main/docs/reference/group_vars.html Demonstrates how to use `group_vars()` to retrieve the grouping variables from a `tidytable` object that has been grouped using `group_by()`. ```R df <- data.table( a = 1:3, b = 4:6, c = c("a", "a", "b"), d = c("a", "a", "b") ) df %>% group_by(c, d) %>% group_vars() #> [1] "c" "d" ``` -------------------------------- ### R Examples for tidytable map Functions Source: https://github.com/markfairbanks/tidytable/blob/main/docs/reference/map2..html Illustrates the practical application of `map`, `map_dbl`, and `map_chr` functions from the `tidytable` package. These examples demonstrate how to apply a simple transformation (`.x + 1`) or type conversion (`as.character`) to a numeric vector, showcasing how different `map` variants return results in various data types (list, double vector, character vector). ```R map(c(1,2,3), ~ .x + 1) #> [[1]] #> [1] 2 #> #> [[2]] #> [1] 3 #> #> [[3]] #> [1] 4 map_dbl(c(1,2,3), ~ .x + 1) #> [1] 2 3 4 map_chr(c(1,2,3), as.character) #> [1] "1" "2" "3" ``` -------------------------------- ### R: Examples of tidytable map Variants Source: https://github.com/markfairbanks/tidytable/blob/main/docs/reference/pmap..html Illustrates the usage of `tidytable`'s `map`, `map_dbl`, and `map_chr` functions. These examples demonstrate how to apply simple transformations to numeric vectors and convert types, showcasing the different return formats (list, double vector, character vector). ```R map(c(1,2,3), ~ .x + 1) #> [[1]] #> [1] 2 #> #> [[2]] #> [1] 3 #> #> [[3]] #> [1] 4 ``` ```R map_dbl(c(1,2,3), ~ .x + 1) #> [1] 2 3 4 ``` ```R map_chr(c(1,2,3), as.character) #> [1] "1" "2" "3" ``` -------------------------------- ### tidytable Function API Reference Source: https://github.com/markfairbanks/tidytable/blob/main/docs/reference/tidytable.html Detailed API documentation for the `tidytable` function, outlining its signature and parameters for constructing data.table objects with improved printing. ```APIDOC tidytable(..., .name_repair = "unique") Arguments: ...: A set of name-value pairs .name_repair: Treatment of duplicate names. See `?vctrs::vec_as_names` for options/details. ``` -------------------------------- ### Example Usage of tidytable::case_when. with mutate Source: https://github.com/markfairbanks/tidytable/blob/main/docs/reference/case_when..html This R example demonstrates how to use `case_when.` within a `mutate` call to create a new column `case_x` based on conditional logic applied to column `x`. It shows how values are assigned based on `x < 5`, `x < 7`, and a default `TRUE` condition. ```R df <- tidytable(x = 1:10) df %>% mutate(case_x = case_when(x < 5 ~ 1, x < 7 ~ 2, TRUE ~ 3)) #> # A tidytable: 10 × 2 #> x case_x #> #> 1 1 1 #> 2 2 1 #> 3 3 1 #> 4 4 1 #> 5 5 2 #> 6 6 2 #> 7 7 3 #> 8 8 3 #> 9 9 3 #> 10 10 3 ``` -------------------------------- ### New Functions for Connection and Environment Management Source: https://github.com/markfairbanks/tidytable/blob/main/revdep/library.noindex/tidytable/new/withr/NEWS.md Several new functions have been added to automatically manage connections and environments. These include `with_connection()` for R file connections, `with_db_connection()` for DBI database connections, and `with_package()`, `with_namespace()`, `with_environment()` (and their local variants) for running code with modified object search paths. A `with_gctorture2` command is also introduced for testing. ```APIDOC Function: with_connection(con: connection, code: expression) Description: Automatically closes R file connections upon exit. Function: with_db_connection(con: DBIConnection, code: expression) Description: Automatically disconnects from DBI database connections upon exit. Command: with_gctorture2(code: expression) Description: Runs code with gctorture2 enabled, useful for testing memory management. Functions: with_package(pkg: string, code: expression), with_namespace(ns: string, code: expression), with_environment(env: environment, code: expression) Description: Run code with a modified object search path. Corresponding `local_` variants are also available. Functions: with_tempfile(path: string, code: expression), local_tempfile(path: string) Description: Create temporary files that are automatically cleaned up afterwards. ``` -------------------------------- ### R tidytable: Examples of drop_na Usage Source: https://github.com/markfairbanks/tidytable/blob/main/docs/reference/drop_na..html Practical examples demonstrating the use of `drop_na` in R with `tidytable`. The first example initializes a `data.table` and shows basic usage, while subsequent examples illustrate dropping `NA`s from specific columns or using `tidyselect` helpers. ```R df <- data.table( x = c(1, 2, NA), y = c("a", NA, "b") ) df %>% drop_na() ``` ```R df %>% drop_na(x) ``` ```R df %>% drop_na(where(is.numeric)) ``` -------------------------------- ### R tidytable::uncount Usage Examples Source: https://github.com/markfairbanks/tidytable/blob/main/docs/reference/uncount.html Practical examples demonstrating how to use the `uncount` function in R. The first example shows basic row expansion, and the second illustrates how to add a unique identifier column to the uncounted rows. ```R df <- data.table(x = c("a", "b"), n = c(1, 2)) uncount(df, n) #> # A tidytable: 3 × 1 #> x #> #> 1 a #> 2 b #> 3 b ``` ```R uncount(df, n, .id = "id") #> # A tidytable: 3 × 2 #> x id #> #> 1 a 1 #> 2 b 1 #> 3 b 2 ``` -------------------------------- ### R tidytable map Family Usage Examples Source: https://github.com/markfairbanks/tidytable/blob/main/docs/reference/map_dbl..html Illustrates practical usage of `map`, `map_dbl`, and `map_chr` functions from the `tidytable` package. These examples demonstrate how to apply simple transformations to numeric vectors and convert elements to different data types, showcasing the varied return types of the `map` function variants. ```R map(c(1,2,3), ~ .x + 1) #> [[1]] #> [1] 2 #> #> [[2]] #> [1] 3 #> #> [[3]] #> [1] 4 ``` ```R map_dbl(c(1,2,3), ~ .x + 1) #> [1] 2 3 4 ``` ```R map_chr(c(1,2,3), as.character) #> [1] "1" "2" "3" ```