tidyr

repository·main·Indexed 23 days ago

https://github.com/tidyverse/tidyr

A core tidyverse package for creating 'tidy data'. It provides tools for reshaping, nesting, and cleaning data structures to ensure each variable is a column, each observation is a row, and each value is a single cell. Key functional categories include pivoting (pivot_longer, pivot_wider), rectangling (unnest_longer, unnest_wider, hoist), nesting/unnesting (nest, unnest), splitting and combining (separate_wider_delim, unite), and handling missing values (complete, drop_na, fill, replace_na).

Tokens
2.5K
Snippets
10
Records
14
Agent score
79%

What's inside tidyr

  1. Overview of tidyr function categories

    main

    The tidyr API is organized into five main functional categories:

    • Pivoting: Converts data between long and wide forms. Use pivot_longer() and pivot_wider() (replaces the deprecated gather() and spread()).
    • Rectangling: Transforms deeply nested lists (e.g., from JSON) into tidy tibbles. Key functions include unnest_longer(), unnest_wider(), and hoist().
    • Nesting/Unnesting: Converts grouped data into a form where each group is a single row containing a nested data frame, and vice versa. Key functions include nest() and unnest().
    • Splitting and Combining: Manipulates character columns. Use separate_wider_delim(), separate_wider_position(), or separate_wider_regex() to split one column into many, and unite() to combine multiple columns into one.
    • Handling Missing Values: Manages implicit and explicit missing values. Use complete() to make implicit values explicit, drop_na() to remove missing values, fill() to replace missing values with adjacent ones, or replace_na() to replace them with a specific known value.
  2. What is tidy data?

    main

    The goal of tidyr is to help you create tidy data. Data is considered tidy when it follows these three principles:

    1. Each variable is a column; each column is a variable.
    2. Each observation is a row; each row is an observation.
    3. Each value is a cell; each cell is a single value.

    Ensuring your data is tidy allows for smoother integration with other tools in the tidyverse.

  3. Prioritize package investigations using CRAN download counts

    main

    When managing tidyr reverse dependency (revdep) checks, you can prioritize which problematic packages to investigate first by analyzing their CRAN download volume from the past month. This helps identify which failures affect the largest number of users.

    To implement this, you can:

    1. Extract package names from a revdep problems report (e.g., problems.md).
    2. Use cranlogs::cran_downloads() to fetch download statistics for those packages.
    3. Calculate the proportion and cumulative proportion of total downloads to identify high-impact packages.
    library(tidyverse)
    
    # 1. Extract package names from the problems report
    new_problems_path <- here::here("revdep/problems.md")
    md <- readLines(new_problems_path)
    pkg <- md %>%
      str_subset("^#[^#]") %>%
      str_extract("[[:alnum:]]+")
    
    # 2. Get download counts for the last month
    dl <- cranlogs::cran_downloads(when = "last-month", packages = pkg)
    
    # 3. Aggregate and calculate proportions
    dl_count <- dl %>%
      count(package, wt = count) %>%
      mutate(package = fct_reorder(package, n)) %>%
      arrange(desc(package))
    
    # Display top packages with cumulative proportion
    dl_count %>%
      mutate(
        prop = n / sum(n),
        cum_prop = cumsum(prop)
      ) %>%
      print(n = 20)
    
    # 4. Visualize the top 20 packages
    ggplot(head(dl_count, 20), aes(package, n)) +
      geom_col() +
      coord_flip()
  4. Install tidyr

    main

    You can install tidyr using several methods depending on your needs:

    1. Install the entire tidyverse: This is the easiest way if you want all tidyverse packages.
    2. Install only tidyr: Use this if you want to keep your environment lightweight.
    3. Install the development version: Use pak to install directly from GitHub.
    # The easiest way to get tidyr is to install the whole tidyverse:
    install.packages("tidyverse")
    
    # Alternatively, install just tidyr:
    install.packages("tidyr")
    
    # Or the development version from GitHub:
    # install.packages("pak")
    pak::pak("tidyverse/tidyr")
  5. Troubleshoot missing 'rstan' dependency in 'ESTER'

    main

    The package ESTER (version 0.2.0) fails installation in both Devel and CRAN environments. The error occurs during the lazy loading phase because the package rstan is missing from the environment.

    To investigate further, run:

    revdepcheck::cloud_details(, "ESTER")
    Error in loadNamespace(j <- imp[[1L]], c(lib.loc, .libPaths()), versionCheck = vI[[j]]) : 
      there is no package called ‘rstan’
    Calls: <Anonymous> ... loadNamespace -> withRestarts -> withOneRestart -> doWithOneRestart
    Execution halted
    ERROR: lazy loading failed for package ‘ESTER’
  6. Troubleshoot BayesPostEst dependency errors

    main

    The BayesPostEst package requires the rstanarm package. If rstanarm is not installed, the dependency check will fail with an error.

    Error message: Package required but not available: ‘rstanarm’

    Package required but not available: ‘rstanarm’
  7. Troubleshoot autoTS dependency errors

    main

    When installing autoTS, you may encounter an error indicating that a required package is missing. Specifically, the installation fails if the prophet package is not available in your environment.

    Error message: Package required but not available: ‘prophet’

    Package required but not available: ‘prophet’
  8. Troubleshoot missing 'clusterProfiler' dependency in 'genekitr'

    main

    The package genekitr (version 1.1.0) fails dependency checks in both Devel and CRAN environments because the required package clusterProfiler is not available.

    To investigate further, run:

    revdepcheck::cloud_details(, "genekitr")
    * checking package dependencies ... ERROR
    Package required but not available: ‘clusterProfiler’
    
    See section ‘The DESCRIPTION file’ in the ‘Writing R Extensions’
    manual.
    * DONE
    Status: 1 ERROR
  9. Troubleshoot dependency errors in 'escalation'

    main

    The package escalation (version 0.1.4) fails dependency checks in both Devel and CRAN environments because the required package trialr is not available. This error is reported during the package dependency check phase.

    To investigate further, run:

    revdepcheck::cloud_details(, "escalation")
    * checking package dependencies ... ERROR
    Package required but not available: ‘trialr'
    
    See section ‘The DESCRIPTION file’ in the ‘Writing R Extensions’
    manual.
    * DONE
    Status: 1 ERROR
  10. Troubleshoot missing 'readMzXmlData' dependency in 'FAMetA'

    main

    The package FAMetA (version 0.1.5) fails installation in both Devel and CRAN environments. The error occurs during lazy loading because the namespace LipidMS fails to load due to a missing readMzXmlData package.

    To investigate further, run:

    revdepcheck::cloud_details(, "FAMetA")
    Error: package or namespace load failed for ‘LipidMS’ in loadNamespace(j <- i[[1L]], c(lib.loc, .libPaths()), versionCheck = vI[[j]]):
     there is no package called ‘readMzXmlData’
    Execution halted
    ERROR: lazy loading failed for package ‘FAMetA’
  11. Troubleshoot bayesnec installation failures

    main

    The bayesnec package may fail during installation (both Devel and CRAN versions) due to missing dependencies required for lazy loading. The installation process fails when it attempts to load the brms namespace but cannot find the rstan package.

    Error message: Error: package or namespace load failed for ‘brms’ in loadNamespace(...): there is no package called ‘rstan’

    Error: package or namespace load failed for ‘brms’ in loadNamespace(j <- imp[[1L]], c(lib.loc, .libPaths()), versionCheck = vI[[j]]):
     there is no package called ‘rstan’
  12. Get detailed dependency information with revdepcheck::cloud_details()

    main

    To retrieve detailed information about a package's dependency status and cloud check details, use the revdepcheck::cloud_details() function. This is useful when investigating failures or dependency issues reported in reverse dependency checks.

    revdepcheck::cloud_details(, "marginaleffects")