tidytable

repository·main·Indexed 19 days ago

https://github.com/markfairbanks/tidytable

A data frame manipulation library that provides tidyverse-like syntax (dplyr/tidyr) while using the high-performance data.table engine in the background. It supports tidyselect helpers, tidy evaluation, and the .by argument for grouping, as well as a dt() helper to mix standard data.table syntax within pipe chains.

Tokens
959
Snippets
6
Records
6
Agent score
17%

What's inside tidytable

  1. How grouping works with the .by argument

    main

    In tidytable, you can use the .by argument in functions like summarize(), mutate(), and filter() as a shorthand for the group_by() %>% ... %>% ungroup() workflow. This reduces typing and avoids leaving the data frame in a grouped state.

    • Pass a single column: .by = z
    • Pass multiple columns: .by = c(y, z)
    df <- data.table(x = c("a", "a", "b"), y = c("a", "a", "b"), z = 1:3)
    
    df %>%
      summarize(avg_z = mean(z), .by = c(x, y))
  2. Use tidytable syntax for data manipulation

    main

    tidytable replicates tidyverse syntax (like dplyr and tidyr) but uses data.table as the high-performance engine in the background. You can replace existing dplyr code by loading the library.

    Commonly used functions include select(), filter(), arrange(), and mutate().

    library(tidytable)
    
    df <- data.table(x = 1:3, y = 4:6, z = c("a", "a", "b"))
    
    df %>%
      select(x, y, z) %>%
      filter(x < 4, y > 1) %>%
      arrange(x, y) %>%
      mutate(double_x = x * 2,
             x_plus_y = x + y)
  3. Use tidyselect for column selection

    main

    tidytable supports tidyselect helpers, allowing you to select or drop columns using standard tidyverse patterns like everything(), starts_with(), ends_with(), any_of(), and where().

    These helpers also work within the .by argument for grouping.

    # Standard selection
    df %>% select(a, starts_with("b"))
    
    # Selection within .by
    df %>% summarize(avg_z = mean(z), .by = where(is.character))
    df <- data.table(
      a = 1:3,
      b1 = 4:6,
      b2 = 7:9,
      c = c("a", "a", "b")
    )
    
    df %>%
      select(a, starts_with("b"))
  4. Use tidy evaluation in custom functions

    main

    tidytable is compatible with tidy evaluation. When writing custom functions, you can use the embracing shortcut {{ }} or enquo() with !! to handle column names. The .data and .env pronouns are also supported.

    # Using {{ }}
    add_one <- function(data, add_col) {
      data %>%
        mutate(new_col = {{ add_col }} + 1)
    }
    
    # Using .data and .env
    df %>%
      mutate(new_col = .data$x + .env$var)
    add_one <- function(data, add_col) {
      data %>%
        mutate(new_col = {{ add_col }} + 1)
    }
    
    df %>%
      add_one(x)
  5. Mix tidytable and data.table syntax with dt()

    main

    The dt() helper allows you to use standard data.table syntax (like DT[i, j, by]) within a tidytable pipe chain. This is useful for leveraging specific data.table features while maintaining a readable pipeline.

    df %>%
      dt(, .(x, y, z)) %>%           # Select columns
      dt(x < 4 & y > 1) %>%          # Filter rows
      dt(order(x, y)) %>%            # Arrange
      dt(, double_x := x * 2) %>%     # Mutate (in-place style)
      dt(, .(avg_x = mean(x)), by = z) # Summarize by group
    df <- data.table(x = 1:3, y = 4:6, z = c("a", "a", "b"))
    
    df %>%
      dt(, .(x, y, z)) %>%
      dt(x < 4 & y > 1) %>%
      dt(order(x, y)) %>%
      dt(, double_x := x * 2) %>%
      dt(, .(avg_x = mean(x)), by = z)