add_tibble_to_list: Add a tibble to a list

View source: R/format.R

add_tibble_to_listR Documentation

Add a tibble to a list

Description

This function prepends tibbles to a list, creating the list in the environment calling this function if not already present.

Usage

add_tibble_to_list(.data, .name)

Arguments

.data

The tibble to be added to the beginning of the list.

.name

The name of the tibble to be added to the beginning of the list.

Details

Adding a tibble to a list is an example of assigning a variable to an environment.

Value

The tibble is returned invisibly so that the function may be used within a pipe workflow.

Examples

suppressPackageStartupMessages({
  suppressWarnings({
    library(palmerpenguins)
    library(dplyr)
  })
})

# select top 5 heaviest penguins from each species on each island
heaviest_penguins <- penguins %>%
  select(species, island, body_mass_g) %>%
  group_by(species, island) %>%
  arrange(desc(body_mass_g)) %>%
  slice_head(n = 5) %>%
  ungroup()
heaviest_penguins

# prepend heaviest penguin species tibble to list
suppressPackageStartupMessages({
  suppressWarnings({
   library(forcats)
  })
})

heaviest_penguins %>%
  filter(species == "Adelie") %>%
  mutate(species = fct_drop(species)) %>%
  add_tibble_to_list("adelie")

heaviest_penguins %>%
  filter(species == "Chinstrap") %>%
  mutate(species = fct_drop(species)) %>%
  add_tibble_to_list("chinstrap")

heaviest_penguins %>%
  filter(species == "Gentoo") %>%
  mutate(species = fct_drop(species)) %>%
  add_tibble_to_list("gentoo")
str(tibble_list, max.level = 1, list.len = 3)

# convert list to tibble
bind_rows(tibble_list)

gcfrench/store documentation built on May 17, 2024, 5:52 p.m.