extract_space_delimited_list | R Documentation |
This function extracts a space 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.
extract_space_delimited_list(
.df,
column_name,
sort = FALSE,
unique_list = FALSE,
last_delimiter = "",
...
)
.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. |
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.
The space separated delimited list as a character string.
Other delimited functions:
extract_comma_delimited_list()
,
extract_paragraph_delimited_list()
,
extract_semicolon_delimited_list()
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
library(purrr)
heaviest_penguins %>%
group_nest(across(c(species:island)), .key = "penguins") %>%
mutate(weight = map_chr(penguins, extract_space_delimited_list, column_name = "body_mass_g")) %>%
select(-penguins)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.