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

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.

Value

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

Details

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

Examples

suppressPackageStartupMessages({ library(store) 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
#> # A tibble: 25 x 3 #> species island body_mass_g #> <fct> <fct> <int> #> 1 Adelie Biscoe 4775 #> 2 Adelie Biscoe 4725 #> 3 Adelie Biscoe 4600 #> 4 Adelie Biscoe 4400 #> 5 Adelie Biscoe 4300 #> 6 Adelie Dream 4650 #> 7 Adelie Dream 4600 #> 8 Adelie Dream 4475 #> 9 Adelie Dream 4450 #> 10 Adelie Dream 4400 #> # ... with 15 more rows
# 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)
#> List of 3 #> $ adelie : tibble [15 x 3] (S3: tbl_df/tbl/data.frame) #> $ chinstrap: tibble [5 x 3] (S3: tbl_df/tbl/data.frame) #> $ gentoo : tibble [5 x 3] (S3: tbl_df/tbl/data.frame)
# convert list to tibble bind_rows(tibble_list)
#> # A tibble: 25 x 3 #> species island body_mass_g #> <fct> <fct> <int> #> 1 Adelie Biscoe 4775 #> 2 Adelie Biscoe 4725 #> 3 Adelie Biscoe 4600 #> 4 Adelie Biscoe 4400 #> 5 Adelie Biscoe 4300 #> 6 Adelie Dream 4650 #> 7 Adelie Dream 4600 #> 8 Adelie Dream 4475 #> 9 Adelie Dream 4450 #> 10 Adelie Dream 4400 #> # ... with 15 more rows