dtplyr Documentation

repository·main·Indexed 20 days ago

https://github.com/tidyverse/dtplyr

dtplyr provides a data.table backend for dplyr, automatically translating familiar dplyr code into high-performance data.table code. It features lazy evaluation via lazy_dt(), allowing users to write standard dplyr pipelines and execute them using as.data.table(), as.data.frame(), or as_tibble().

Tokens
696
Snippets
4
Records
5
Agent score
22%

What's inside dtplyr

  1. How dtplyr works

    main
    dtplyr provides a data.table backend for dplyr. It allows you to write standard dplyr code which is then automatically translated into highly efficient data.table code. This combines the user-friendly syntax of dplyr with the high performance of data.table.
  2. Use dtplyr to translate dplyr code to data.table

    main

    To use dtplyr, load dtplyr, dplyr, and optionally data.table. Use lazy_dt() to wrap a data frame into a "lazy" data table. This object tracks all subsequent dplyr operations without executing them immediately. To execute the operations and retrieve the results, use as.data.table(), as.data.frame(), or as_tibble().

    library(data.table)
    library(dtplyr)
    library(dplyr, warn.conflicts = FALSE)
    
    # Create a lazy data table
    mtcars2 <- lazy_dt(mtcars)
    
    # Perform transformations and execute to get a tibble
    result <- mtcars2 %>%
      filter(wt < 5) %>%
      mutate(l100k = 235.21 / mpg) %>%
      group_by(cyl) %>%
      summarise(l100k = mean(l100k)) %>%
      as_tibble()
  3. Install dtplyr

    main

    You can install the stable version of dtplyr from CRAN or the development version from GitHub using pak.

    # From CRAN
    install.packages("dtplyr")
    
    # From GitHub (development version)
    # install.packages("pak")
    pak::pak("tidyverse/dtplyr")
  4. Preview generated data.table code

    main

    You can inspect the data.table code that dtplyr has generated for your dplyr pipeline by simply printing the lazy object. This is useful for debugging and verifying the translation.

    mtcars2 %>%
      filter(wt < 5) %>%
      mutate(l100k = 235.21 / mpg) %>%
      group_by(cyl) %>%
      summarise(l100k = mean(l100k))
    # This will print the Source, Call (the data.table syntax), and the result
  5. Optimize performance with immutable = FALSE

    main

    By default, dtplyr follows dplyr semantics where mutate() does not modify the object in place, which may require making copies of the data. To allow in-place modification (matching data.table behavior) and potentially improve performance, you can set immutable = FALSE when calling lazy_dt().

    # Example of opting out of immutability
    mtcars2 <- lazy_dt(mtcars, immutable = FALSE)