datawizard

repository·main·Indexed 42 days ago

https://github.com/easystats/datawizard

A lightweight, dependency-free R package for data wrangling and statistical transformations. Part of the easystats ecosystem, it provides string-friendly alternatives to tidyverse-style manipulation for selecting, filtering, reshaping, and merging data frames. It includes specialized functions for statistical preprocessing such as standardize(), winsorize(), ranktransform(), and describe_distribution() for comprehensive descriptive summaries.

Tokens
4.6K
Snippets
22
Records
23
Agent score
51%

What's inside datawizard

  1. Overview of datawizard features

    main

    datawizard is a lightweight R package designed for data manipulation and statistical transformations. It is part of the easystats ecosystem.

    It focuses on two main areas:

    1. Data manipulation: Provides functions similar to tidyverse (dplyr, tidyr) for selecting, filtering, and reshaping data. Key characteristics include:
      • All functions use the data_* prefix for easy identification.
      • Functions are "string-friendly," making them easy to use within other functions or programmatic workflows.
      • Extremely lightweight with no dependencies.
    2. Statistical transformations: Includes powerful functions for common transformations such as standardization, normalization, rescaling, rank-transformation, scale reversing, recoding, and binning.
  2. Understand the datawizard function design and pipe-workflow

    main

    The {datawizard} package is designed for seamless integration into R pipe-workflows (|>). Most functions follow a consistent argument pattern:

    1. The first argument is always the data.
    2. For functions operating on data frames, the next two arguments are select and exclude for variable manipulation.
    3. Subsequent arguments are task-specific parameters.

    Functions that accept data frames typically return a modified data frame, allowing you to chain multiple operations together.

    iris |>
      data_filter(Species %in% c("versicolor", "virginica")) |>
      data_select(contains("\\.")) |>
      data_relocate(ends_with("Length")) |>
      data_remove(4) |>
      head()
  3. Apply Statistical Transformations

    main

    After data preparation, use {datawizard} to apply preprocessing steps and statistical transformations required by specific statistical tests. These functions allow you to modify variable distributions to meet model assumptions.

    Key functions include:

    • standardize(): Center and scale data.
    • normalize(): Scale variables to a 0-1 range.
    • adjust(): Adjust data for the effect of other variables.
    • slide(): Shift numeric value ranges.
    • ranktransform(): Convert numeric values to integer ranks.
    d <- data.frame(
      a = c(-2, -1, 0, 1, 2),
      b = c(3, 4, 5, 6, 7)
    )
    
    # Standardize data with specific center and scale values
    standardize(d, center = c(3, 4), scale = c(2, 4))
  4. Data Preparation with data_ functions

    main

    Use {datawizard} to clean and reshape raw data for statistical modeling. The package provides functions for selecting observations, selecting variables, renaming, and reshaping data. The syntax is designed to be similar to the {tidyverse} ecosystem.

    Key functions include:

    • data_filter(): Select specific observations.
    • data_select(): Select specific variables.
    • data_extract(): Extract a single variable.
    • data_rename(): Rename variables.
    • data_to_long(): Convert data from wide to long format.
    • data_to_wide(): Convert data from long to wide format.
    • data_join(): Join two data frames.
    stocks <- data.frame(
      time = as.Date("2009-01-01") + 0:4,
      X = rnorm(5, 0, 1),
      Y = rnorm(5, 0, 2)
    )
    
    # Convert wide data to long format
    data_to_long(
      stocks,
      select = -c("time"),
      names_to = "stock",
      values_to = "price"
    )
  5. Install datawizard

    main

    You can install datawizard from CRAN, the easystats r-universe, or directly from GitHub.

    Tip: Instead of loading library(datawizard), it is recommended to use library(easystats) to make all features of the easystats ecosystem available. To keep your packages updated, use easystats::install_latest().

    # Release version from CRAN
    install.packages("datawizard")
    
    # Development version from r-universe
    install.packages("datawizard", repos = "https://easystats.r-universe.dev")
    
    # Development version from GitHub
    remotes::install_github("easystats/datawizard")
  6. How datawizard functions are designed

    main

    The {datawizard} functions follow a consistent design principle to facilitate easy learning and seamless integration into R pipe-workflows (|>).

    1. Argument Order: The first argument is always the data.
    2. Data Frame Methods: For functions working on data frames, the next two arguments are typically used to select and exclude variables.
    3. Task Arguments: Subsequent arguments are specific to the function's task.
    4. Pipe-friendly: Functions that accept data frames usually return a modified data frame, allowing them to be chained together.
    iris |>
      data_filter(Species %in% c("versicolor", "virginica")) |
      data_select(contains("\.")) |
      data_relocate(ends_with("Length")) |
      data_remove(4) |
      head()
  7. Chain data wrangling operations in a pipe

    main

    You can combine various {datawizard} functions to transform your data in a single workflow. Common operations include:

    • data_filter(): Filter rows based on conditions.
    • data_select(): Select specific columns (often using pattern matching like contains()).
    • data_relocate(): Change the order of columns.
    • data_remove(): Remove columns by index or name.

    Example workflow:

    iris |>
      data_filter(Species %in% c("versicolor", "virginica")) |>
      data_select(contains("\\.")) |>
      data_relocate(ends_with("Length")) |>
      data_remove(4) |>
      head()
  8. Get descriptive summaries with describe_distribution()

    main

    Use describe_distribution() to generate a comprehensive descriptive summary for all variables in a dataframe or for a single specific variable. The output includes statistics such as Mean, SD, IQR, Range, Skewness, Kurtosis, sample size (n), and missing values (n_Missing).

    # For a whole dataframe
    data(iris)
    describe_distribution(iris)
    
    # For a single variable
    describe_distribution(mtcars$wt)
  9. Reorder and rename columns

    main

    Use data_relocate() to change the position of columns and data_rename() to change their names.

    • data_relocate(df, select, before): Moves the specified select columns to a position before the before column.
    • data_rename(df, old_names, new_names): Renames columns by passing a vector of current names and a vector of new names.
    # Move 'Species' to the front, before 'Sepal.Length'
    data_relocate(iris, select = "Species", before = "Sepal.Length")
    
    # Rename specific columns
    data_rename(iris, c("Sepal.Length", "Sepal.Width"), c("length", "width"))
  10. Apply statistical transformations

    main

    The package includes several functions for common statistical data transformations:

    • standardize(df): Performs z-score standardization on numeric data.
    • winsorize(df): Applies winsorization to handle outliers.
    • center(df): Performs grand-mean centering.
    • ranktransform(df): Applies a rank transformation to the data.
    • change_scale(vec, new_min, new_max): Rescales a numeric vector to a new specified range (defaults to 0-100 if not specified).
    • categorize(vec, split, n_groups): Recodes data into groups. For example, using split = "quantile" to create groups based on quantiles.
    # Standardize data
    standardize(swiss)
    
    # Winsorize data
    winsorize(anscombe)
    
    # Center data
    center(anscombe)
    
    # Rescale a vector
    change_scale(c(0, 1, 5, -5, -2))
    
    # Categorize into quantiles
    categorize(x, split = "quantile", n_groups = 3)
  11. Summarize data properties with describe_distribution()

    main

    The describe_distribution() function provides a comprehensive summary of data properties and distributions. It computes various indices for each variable in a data frame, including measures of centrality, dispersion, range, skewness, and kurtosis.

    Output includes:

    • Mean, SD, IQR
    • Min, Max
    • Skewness, Kurtosis
    • Sample size (n) and missing values (n_Missing)
    describe_distribution(mtcars)
  12. Calculate data smoothness with smoothness()

    main

    The smoothness() function computes additional data properties. You can specify the calculation method using the method argument (e.g., method = "diff").

    x <- (-10:10)^3 + rnorm(21, 0, 100)
    smoothness(x, method = "diff")