extract_comma_delimited_list: Extract a comma delimited list of items

extract_comma_delimited_listR Documentation

Extract a comma delimited list of items

Description

This function extracts a comma delimited list from a data frame's column. It excludes NAs, optionally removes duplicate elements, sorts the character string and adds text as a delimiter to the last two items.

Usage

extract_comma_delimited_list(
  .df,
  column_name,
  sort = FALSE,
  unique_list = FALSE,
  last_delimiter = "",
  ...
)

Arguments

.df

A data frame containing the column with the delimited list to extract.

column_name

The name of the column containing the delimited list to extract.

sort

Should the delimited list be sorted (TRUE) or not (default FALSE).

unique_list

Should the delimited list contain duplicated elements (default FALSE) or not (TRUE).

last_delimiter

An optional character string used to separate the last two items in the delimited list.

Details

It is an example of the use of a function factory, tidy evaluation and purrr's map function. The function may be called on a nested data frame to extract the delimited list.

Value

The comma separated delimited list as a character string.

See Also

Other delimited functions: extract_paragraph_delimited_list(), extract_semicolon_delimited_list(), extract_space_delimited_list()

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

# extract comma separated list of penguin weights for each species on each island
suppressPackageStartupMessages({
  suppressWarnings({
   library(purrr)
  })
})

heaviest_penguins %>%
 group_nest(across(c(species:island)), .key = "penguins") %>%
 mutate(weight = map_chr(penguins, extract_comma_delimited_list, column_name = "body_mass_g")) %>%
 select(-penguins)

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