Tidytable
Tidytable
URL https://markfairbanks.github.io/tidytable/,
https://github.com/markfairbanks/tidytable
BugReports https://github.com/markfairbanks/tidytable/issues
Suggests testthat (>= 2.1.0), bit64, knitr, rmarkdown, crayon
NeedsCompilation no
Author Mark Fairbanks [aut, cre],
Abdessabour Moutik [ctb],
Matt Carlson [ctb],
Ivan Leung [ctb],
Ross Kennedy [ctb],
Robert On [ctb],
Alexander Sevostianov [ctb],
Koen ter Berg [ctb]
Maintainer Mark Fairbanks <mark.t.fairbanks@gmail.com>
Repository CRAN
Date/Publication 2024-12-11 10:20:02 UTC
1
2 Contents
Contents
across . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
add_count . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
arrange . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
as_tidytable . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
between . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
bind_cols . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
case . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
case_match . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
case_when . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
coalesce . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
complete . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
consecutive_id . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
context . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
count . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
crossing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
cross_join . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
c_across . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
desc . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
distinct . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
drop_na . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
dt . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
enframe . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
expand . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
expand_grid . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
extract . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
fill . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
filter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
first . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
fread . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
get_dummies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
group_by . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
group_cols . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
group_split . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
group_vars . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
if_all . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
if_else . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
inv_gc . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
is_grouped_df . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
is_tidytable . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
lag . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
left_join . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
map . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
mutate . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
mutate_rowwise . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
n . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38
na_if . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38
across 3
nest . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
nest_by . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40
nest_join . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
new_tidytable . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
n_distinct . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
pick . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
pivot_longer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
pivot_wider . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44
pull . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
reframe . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
relocate . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
rename . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
rename_with . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
replace_na . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
rowwise . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50
row_number . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51
select . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52
separate . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53
separate_longer_delim . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54
separate_rows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54
separate_wider_delim . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55
separate_wider_regex . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56
slice_head . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57
summarize . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58
tidytable . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60
top_n . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60
transmute . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
tribble . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62
uncount . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62
unite . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63
unnest . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64
unnest_longer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65
unnest_wider . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66
%in% . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67
Index 68
Description
Apply a function across a selection of columns. For use in arrange(), mutate(), and summarize().
Usage
across(.cols = everything(), .fns = NULL, ..., .names = NULL)
4 add_count
Arguments
.cols vector c() of unquoted column names. tidyselect compatible.
.fns Function to apply. Can be a purrr-style lambda. Can pass also list of functions.
... Other arguments for the passed function
.names A glue specification that helps with renaming output columns. {.col} stands
for the selected column, and {.fn} stands for the name of the function being
applied. The default (NULL) is equivalent to "{.col}" for a single function case
and "{.col}_{.fn}" when a list is used for .fns.
Examples
df <- data.table(
x = rep(1, 3),
y = rep(2, 3),
z = c("a", "a", "b")
)
df %>%
mutate(across(c(x, y), ~ .x * 2))
df %>%
summarize(across(where(is.numeric), ~ mean(.x)),
.by = z)
df %>%
arrange(across(c(y, z)))
Description
Add a count column to the data frame.
df %>% add_count(a, b) is equivalent to using df %>% mutate(n = n(), .by = c(a, b))
Usage
add_count(.df, ..., wt = NULL, sort = FALSE, name = NULL)
Arguments
.df A data.frame or data.table
... Columns to group by. tidyselect compatible.
wt Frequency weights. Can be NULL or a variable:
arrange 5
Examples
df <- data.table(
a = c("a", "a", "b"),
b = 1:3
)
df %>%
add_count(a)
Description
Order rows in ascending or descending order.
Usage
arrange(.df, ...)
Arguments
.df A data.frame or data.table
... Variables to arrange by
Examples
df <- data.table(
a = 1:3,
b = 4:6,
c = c("a", "a", "b")
)
df %>%
arrange(c, -a)
df %>%
arrange(c, desc(a))
6 between
Description
A tidytable object is simply a data.table with nice printing features.
Note that all tidytable functions automatically convert data.frames & data.tables to tidytables in the
background. As such this function will rarely need to be used by the user.
Usage
as_tidytable(x, ..., .name_repair = "unique", .keep_rownames = FALSE)
Arguments
x An R object
... Additional arguments to be passed to or from other methods.
.name_repair Treatment of duplicate names. See ?vctrs::vec_as_names for options/details.
.keep_rownames Default is FALSE. If TRUE, adds the input object’s names as a separate column
named "rn". .keep_rownames = "id" names the column "id" instead.
Examples
df <- data.frame(x = -2:2, y = c(rep("a", 3), rep("b", 2)))
df %>%
as_tidytable()
between Do the values from x fall between the left and right bounds?
Description
between() utilizes data.table::between() in the background
Usage
between(x, left, right)
Arguments
x A numeric vector
left, right Boundary values
bind_cols 7
Examples
df <- data.table(
x = 1:5,
y = 1:5
)
df %>%
filter(x %>% between(2, 4))
Description
Bind multiple data.tables into one row-wise or col-wise.
Usage
bind_cols(..., .name_repair = "unique")
Arguments
... data.tables or data.frames to bind
.name_repair Treatment of duplicate names. See ?vctrs::vec_as_names for options/details.
.id If TRUE, an integer column is made as a group id
Examples
# Binding data together by row
df1 <- data.table(x = 1:3, y = 10:12)
df2 <- data.table(x = 4:6, y = 13:15)
df1 %>%
bind_rows(df2)
bind_rows(df_list)
df1 %>%
bind_cols(df2)
Description
This function allows you to use multiple if/else statements in one call.
It is called like data.table::fcase(), but allows the user to use a vector as the default argument.
Usage
Arguments
Examples
df %>%
mutate(case_x = case(x < 5, 1,
x < 7, 2,
default = 3))
case_match 9
Description
Allows the user to succinctly create a new vector based off conditions of a single vector.
Usage
case_match(.x, ..., .default = NA, .ptype = NULL)
Arguments
.x A vector
... A sequence of two-sided formulas. The left hand side gives the old values, the
right hand side gives the new value.
.default The default value if all conditions evaluate to FALSE.
.ptype Optional ptype to specify the output type.
Examples
df <- tidytable(x = c("a", "b", "c", "d"))
df %>%
mutate(
case_x = case_match(x,
c("a", "b") ~ "new_1",
"c" ~ "new_2",
.default = x)
)
Description
This function allows you to use multiple if/else statements in one call.
It is called like dplyr::case_when(), but utilizes data.table::fifelse() in the background for
improved performance.
Usage
case_when(..., .default = NA, .ptype = NULL, .size = NULL)
10 coalesce
Arguments
... A sequence of two-sided formulas. The left hand side gives the conditions, the
right hand side gives the values.
.default The default value if all conditions evaluate to FALSE.
.ptype Optional ptype to specify the output type.
.size Optional size to specify the output size.
Examples
df <- tidytable(x = 1:10)
df %>%
mutate(case_x = case_when(x < 5 ~ 1,
x < 7 ~ 2,
TRUE ~ 3))
Description
Fill in missing values in a vector by pulling successively from other vectors.
Usage
coalesce(..., .ptype = NULL, .size = NULL)
Arguments
... Input vectors. Supports dynamic dots.
.ptype Optional ptype to override output type
.size Optional size to override output size
Examples
# Use a single value to replace all missing values
x <- c(1:3, NA, NA)
coalesce(x, 0)
c(NA, NA, 3, 4, 5)
)
coalesce(!!!vecs)
Description
Turns implicit missing values into explicit missing values.
Usage
complete(.df, ..., fill = list(), .by = NULL)
Arguments
.df A data.frame or data.table
... Columns to expand
fill A named list of values to fill NAs with.
.by Columns to group by
Examples
df <- data.table(x = 1:2, y = 1:2, z = 3:4)
df %>%
complete(x, y)
df %>%
complete(x, y, fill = list(z = 10))
Description
Generate a unique id for runs of consecutive values
Usage
consecutive_id(...)
Arguments
... Vectors of values
12 context
Examples
x <- c(1, 1, 2, 2, 1, 1)
consecutive_id(x)
Description
These functions give information about the "current" group.
Usage
cur_column()
cur_data()
cur_group_id()
cur_group_rows()
Examples
df <- data.table(
x = 1:5,
y = c("a", "a", "a", "b", "b")
)
df %>%
mutate(
across(c(x, y), ~ paste(cur_column(), .x))
)
df %>%
summarize(data = list(cur_data()),
.by = y)
df %>%
mutate(group_id = cur_group_id(),
.by = y)
count 13
df %>%
mutate(group_rows = cur_group_rows(),
.by = y)
Description
Returns row counts of the dataset.
tally() returns counts by group on a grouped tidytable.
count() returns counts by group on a grouped tidytable, or column names can be specified to return
counts by group.
Usage
count(.df, ..., wt = NULL, sort = FALSE, name = NULL)
Arguments
.df A data.frame or data.table
... Columns to group by in count(). tidyselect compatible.
wt Frequency weights. tidyselect compatible. Can be NULL or a variable:
• If NULL (the default), counts the number of rows in each group.
• If a variable, computes sum(wt) for each group.
sort If TRUE, will show the largest groups at the top.
name The name of the new column in the output.
If omitted, it will default to n.
Examples
df <- data.table(
x = c("a", "a", "b"),
y = c("a", "a", "b"),
z = 1:3
)
df %>%
count()
df %>%
count(x)
df %>%
14 crossing
count(where(is.character))
df %>%
count(x, wt = z, name = "x_sum")
df %>%
count(x, sort = TRUE)
df %>%
tally()
df %>%
group_by(x) %>%
tally()
Description
Usage
Arguments
Examples
x <- 1:2
y <- 1:2
crossing(x, y)
crossing(stuff = x, y)
cross_join 15
Description
Cross join each row of x to every row in y.
Usage
cross_join(x, y, ..., suffix = c(".x", ".y"))
Arguments
x A data.frame or data.table
y A data.frame or data.table
... Other parameters passed on to methods
suffix Append created for duplicated column names when using full_join()
Examples
df1 <- tidytable(x = 1:3)
df2 <- tidytable(y = 4:6)
cross_join(df1, df2)
Description
c_across() works inside of mutate_rowwise(). It uses tidyselect so you can easily select multiple
variables.
Usage
c_across(cols = everything())
Arguments
cols Columns to transform.
Examples
df <- data.table(x = runif(6), y = runif(6), z = runif(6))
df %>%
mutate_rowwise(row_mean = mean(c_across(x:z)))
16 distinct
Description
Arrange in descending order. Can be used inside of arrange()
Usage
desc(x)
Arguments
x Variable to arrange in descending order
Examples
df <- data.table(
a = 1:3,
b = 4:6,
c = c("a", "a", "b")
)
df %>%
arrange(c, desc(a))
Description
Retain only unique/distinct rows from an input df.
Usage
distinct(.df, ..., .keep_all = FALSE)
Arguments
.df A data.frame or data.table
... Columns to select before determining uniqueness. If omitted, will use all columns.
tidyselect compatible.
.keep_all Only relevant if columns are provided to ... arg. This keeps all columns, but
only keeps the first row of each distinct values of columns provided to ... arg.
drop_na 17
Examples
df <- tidytable(
x = 1:3,
y = 4:6,
z = c("a", "a", "b")
)
df %>%
distinct()
df %>%
distinct(z)
Description
Drop rows containing missing values
Usage
drop_na(.df, ...)
Arguments
.df A data.frame or data.table
... Optional: A selection of columns. If empty, all variables are selected. tidyselect
compatible.
Examples
df <- data.table(
x = c(1, 2, NA),
y = c("a", NA, "b")
)
df %>%
drop_na()
df %>%
drop_na(x)
df %>%
drop_na(where(is.numeric))
18 dt
Description
Usage
dt(.df, i, j, ...)
Arguments
Examples
df <- tidytable(
x = 1:3,
y = 4:6,
z = c("a", "a", "b")
)
df %>%
dt(, double_x := x * 2) %>%
dt(order(-double_x))
df %>%
add_one(x)
enframe 19
Description
Converts named and unnamed vectors to a data.table/tidytable.
Usage
enframe(x, name = "name", value = "value")
Arguments
x A vector
name Name of the column that stores the names. If name = NULL, a one-column tidytable
will be returned.
value Name of the column that stores the values.
Examples
vec <- 1:3
names(vec) <- letters[1:3]
enframe(vec)
Description
Generates all combinations of variables found in a dataset.
expand() is useful in conjunction with joins:
• use with right_join() to convert implicit missing values to explicit missing values
• use with anti_join() to find out which combinations are missing
nesting() is a helper that only finds combinations already present in the dataset.
Usage
expand(.df, ..., .name_repair = "check_unique", .by = NULL)
Arguments
Examples
df <- tidytable(x = c(1, 1, 2), y = c(1, 1, 2))
df %>%
expand(x, y)
df %>%
expand(nesting(x, y))
Description
Usage
Arguments
Examples
x <- 1:2
y <- 1:2
expand_grid(x, y)
expand_grid(stuff = x, y)
extract 21
Description
Superseded
extract() has been superseded by separate_wider_regex().
Given a regular expression with capturing groups, extract() turns each group into a new column.
If the groups don’t match, or the input is NA, the output will be NA. When you pass same name in
the into argument it will merge the groups together. Whilst passing NA in the into arg will drop
the group from the resulting tidytable
Usage
extract(
.df,
col,
into,
regex = "([[:alnum:]]+)",
remove = TRUE,
convert = FALSE,
...
)
Arguments
.df A data.table or data.frame
col Column to extract from
into New column names to split into. A character vector.
regex A regular expression to extract the desired values. There should be one group
(defined by ()) for each element of into
remove If TRUE, remove the input column from the output data.table
convert If TRUE, runs type.convert() on the resulting column. Useful if the resulting
column should be type integer/double.
... Additional arguments passed on to methods.
Examples
df <- data.table(x = c(NA, "a-b-1", "a-d-3", "b-c-2", "d-e-7"))
df %>% extract(x, "A")
df %>% extract(x, c("A", "B"), "([[:alnum:]]+)-([[:alnum:]]+)")
# If no match, NA:
df %>% extract(x, c("A", "B"), "([a-d]+)-([a-d]+)")
# drop columns by passing NA
22 fill
Description
Fills missing values in the selected columns using the next or previous entry. Can be done by group.
Supports tidyselect
Usage
Arguments
Examples
df <- data.table(
a = c(1, NA, 3, 4, 5),
b = c(NA, 2, NA, NA, 5),
groups = c("a", "a", "a", "b", "b")
)
df %>%
fill(a, b)
df %>%
fill(a, b, .by = groups)
df %>%
fill(a, b, .direction = "downup", .by = groups)
filter 23
Description
Filters a dataset to choose rows where conditions are true.
Usage
filter(.df, ..., .by = NULL)
Arguments
.df A data.frame or data.table
... Conditions to filter by
.by Columns to group by if filtering with a summary function
Examples
df <- tidytable(
a = 1:3,
b = 4:6,
c = c("a", "a", "b")
)
df %>%
filter(a >= 2, b >= 4)
df %>%
filter(b <= mean(b), .by = c)
Description
Extract the first, last, or nth value from a vector.
Note: These are simple wrappers around vctrs::vec_slice().
Usage
first(x, default = NULL, na_rm = FALSE)
Arguments
x A vector
default The default value if the value doesn’t exist.
na_rm If TRUE ignores missing values.
n For nth(), a number specifying the position to grab.
Examples
first(vec)
last(vec)
nth(vec, 4)
Description
Usage
fread(...)
Arguments
Examples
fread(fake_csv)
get_dummies 25
Description
Convert character and factor columns to dummy variables
Usage
get_dummies(
.df,
cols = where(~is.character(.x) | is.factor(.x)),
prefix = TRUE,
prefix_sep = "_",
drop_first = FALSE,
dummify_na = TRUE
)
Arguments
.df A data.frame or data.table
cols A single column or a vector of unquoted columns to dummify. Defaults to all
character & factor columns using c(where(is.character), where(is.factor)).
tidyselect compatible.
prefix TRUE/FALSE - If TRUE, a prefix will be added to new column names
prefix_sep Separator for new column names
drop_first TRUE/FALSE - If TRUE, the first dummy column will be dropped
dummify_na TRUE/FALSE - If TRUE, NAs will also get dummy columns
Examples
df <- tidytable(
chr = c("a", "b", NA),
fct = as.factor(c("a", NA, "c")),
num = 1:3
)
df %>%
get_dummies(cols = chr)
df %>%
get_dummies(cols = c(chr, fct), drop_first = TRUE)
26 group_by
df %>%
get_dummies(prefix_sep = ".", dummify_na = FALSE)
group_by Grouping
Description
Usage
ungroup(.df, ...)
Arguments
Examples
df <- data.table(
a = 1:3,
b = 4:6,
c = c("a", "a", "b"),
d = c("a", "a", "b")
)
df %>%
group_by(c, d) %>%
summarize(mean_a = mean(a)) %>%
ungroup()
Description
Usage
group_cols()
Examples
df <- tidytable(
x = c("a", "b", "c"),
y = 1:3,
z = 1:3
)
df %>%
group_by(x) %>%
select(group_cols(), y)
Description
Usage
Arguments
Examples
df <- tidytable(
a = 1:3,
b = 1:3,
c = c("a", "a", "b"),
d = c("a", "a", "b")
)
df %>%
group_split(c, d)
df %>%
group_split(c, d, .keep = FALSE)
df %>%
group_split(c, d, .named = TRUE)
Description
Usage
group_vars(x)
Arguments
x A grouped tidytable
Examples
df <- data.table(
a = 1:3,
b = 4:6,
c = c("a", "a", "b"),
d = c("a", "a", "b")
)
df %>%
group_by(c, d) %>%
group_vars()
if_all 29
Description
Helpers to apply a filter across a selection of columns.
Usage
if_all(.cols = everything(), .fns = NULL, ...)
Arguments
.cols Selection of columns
.fns Function to create filter conditions
... Other arguments passed to the function
Examples
iris %>%
filter(if_any(ends_with("Width"), ~ .x > 4))
iris %>%
filter(if_all(ends_with("Width"), ~ .x > 2))
Description
Fast version of base::ifelse().
Usage
if_else(condition, true, false, missing = NA, ..., ptype = NULL, size = NULL)
Arguments
condition Conditions to test on
true Values to return if conditions evaluate to TRUE
false Values to return if conditions evaluate to FALSE
missing Value to return if an element of test is NA
... These dots are for future extensions and must be empty.
ptype Optional ptype to override output type
size Optional size to override output size
30 inv_gc
Examples
x <- 1:5
if_else(x < 3, 1, 0)
df %>%
mutate(new_col = if_else(x < 3, 1, 0))
Description
Run garbage collection without the gc() output. Can also be run in the middle of a long pipe chain.
Useful for large datasets or when using parallel processing.
Usage
inv_gc(x)
Arguments
x Optional. If missing runs gc() silently. Else returns the same object unaltered.
Examples
Description
Usage
is_grouped_df(x)
Arguments
x An object
Examples
df <- data.table(
a = 1:3,
b = c("a", "a", "b")
)
df %>%
group_by(b) %>%
is_grouped_df()
Description
This function returns TRUE for tidytables or subclasses of tidytables, and FALSE for all other
objects.
Usage
is_tidytable(x)
Arguments
x An object
32 lag
Examples
df <- data.frame(x = 1:3, y = 1:3)
is_tidytable(df)
is_tidytable(df)
Description
Find the "previous" or "next" values in a vector. Useful for comparing values behind or ahead of
the current values.
Usage
Arguments
x a vector of values
n a positive integer of length 1, giving the number of positions to lead or lag by
default value used for non-existent rows. Defaults to NA.
Examples
x <- 1:5
lag(x, 1)
lead(x, 1)
df %>%
mutate(lag_x = lag(x))
left_join 33
Description
Usage
anti_join(x, y, by = NULL)
semi_join(x, y, by = NULL)
Arguments
x A data.frame or data.table
y A data.frame or data.table
by A character vector of variables to join by. If NULL, the default, the join will do
a natural join, using all variables with common names across the two tables.
suffix Append created for duplicated column names when using full_join()
... Other parameters passed on to methods
keep Should the join keys from both x and y be preserved in the output?
Examples
df1 <- data.table(x = c("a", "a", "b", "c"), y = 1:4)
df2 <- data.table(x = c("a", "b"), z = 5:6)
Description
The map functions transform their input by applying a function to each element and returning a
list/vector/data.table.
• map() returns a list
• _lgl(), _int, _dbl,_chr, _df variants return their specified type
• _dfr & _dfc Return all data frame results combined utilizing row or column binding
Usage
map(.x, .f, ...)
Arguments
.x A list or vector
.f A function
... Other arguments to pass to a function
.id Whether map_dfr() should add an id column to the finished dataset
.ptype ptype for resulting vector in map_vec()
.y A list or vector
.l A list to use in pmap
Examples
map(c(1,2,3), ~ .x + 1)
map_dbl(c(1,2,3), ~ .x + 1)
map_chr(c(1,2,3), as.character)
36 mutate
Description
With mutate() you can do 3 things:
Usage
mutate(
.df,
...,
.by = NULL,
.keep = c("all", "used", "unused", "none"),
.before = NULL,
.after = NULL
)
Arguments
.df A data.frame or data.table
... Columns to add/modify
.by Columns to group by
.keep experimental: This is an experimental argument that allows you to control which
columns from .df are retained in the output:
• "all", the default, retains all variables.
• "used" keeps any variables used to make new variables; it’s useful for
checking your work as it displays inputs and outputs side-by-side.
• "unused" keeps only existing variables not used to make new variables.
• "none", only keeps grouping keys (like transmute()).
.before, .after Optionally indicate where new columns should be placed. Defaults to the right
side of the data frame.
Examples
df <- data.table(
a = 1:3,
b = 4:6,
c = c("a", "a", "b")
)
mutate_rowwise 37
df %>%
mutate(double_a = a * 2,
a_plus_b = a + b)
df %>%
mutate(double_a = a * 2,
avg_a = mean(a),
.by = c)
df %>%
mutate(double_a = a * 2, .keep = "used")
df %>%
mutate(double_a = a * 2, .after = a)
Description
Allows you to mutate "by row". this is most useful when a vectorized function doesn’t exist.
Usage
mutate_rowwise(
.df,
...,
.keep = c("all", "used", "unused", "none"),
.before = NULL,
.after = NULL
)
Arguments
.df A data.table or data.frame
... Columns to add/modify
.keep experimental: This is an experimental argument that allows you to control which
columns from .df are retained in the output:
• "all", the default, retains all variables.
• "used" keeps any variables used to make new variables; it’s useful for
checking your work as it displays inputs and outputs side-by-side.
• "unused" keeps only existing variables not used to make new variables.
• "none", only keeps grouping keys (like transmute()).
.before, .after Optionally indicate where new columns should be placed. Defaults to the right
side of the data frame.
38 na_if
Examples
df <- data.table(x = 1:3, y = 1:3 * 2, z = 1:3 * 3)
Description
Helper function that can be used to find counts by group.
Can be used inside summarize(), mutate(), & filter()
Usage
n()
Examples
df <- data.table(
x = 1:3,
y = 4:6,
z = c("a","a","b")
)
df %>%
summarize(count = n(), .by = z)
Description
Convert values to NA.
Usage
na_if(x, y)
nest 39
Arguments
x A vector
y Value to replace with NA
Examples
vec <- 1:3
na_if(vec, 3)
Description
Nest columns into a list-column
Usage
nest(.df, ..., .by = NULL, .key = NULL, .names_sep = NULL)
Arguments
.df A data.table or data.frame
... Columns to be nested.
.by Columns to nest by
.key New column name if .by is used
.names_sep If NULL, the names will be left alone. If a string, the names of the columns will
be created by pasting together the inner column names and the outer column
names.
Examples
df <- data.table(
a = 1:3,
b = 1:3,
c = c("a", "a", "b"),
d = c("a", "a", "b")
)
df %>%
nest(data = c(a, b))
df %>%
nest(data = where(is.numeric))
df %>%
nest(.by = c(c, d))
40 nest_by
Description
Usage
Arguments
Examples
df <- data.table(
a = 1:5,
b = 6:10,
c = c(rep("a", 3), rep("b", 2)),
d = c(rep("a", 3), rep("b", 2))
)
df %>%
nest_by()
df %>%
nest_by(c, d)
df %>%
nest_by(where(is.character))
df %>%
nest_by(c, d, .keep = TRUE)
nest_join 41
Description
Join the data from y as a list column onto x.
Usage
nest_join(x, y, by = NULL, keep = FALSE, name = NULL, ...)
Arguments
x A data.frame or data.table
y A data.frame or data.table
by A character vector of variables to join by. If NULL, the default, the join will do
a natural join, using all variables with common names across the two tables.
keep Should the join keys from both x and y be preserved in the output?
name The name of the list-column created by the join. If NULL the name of y is used.
... Other parameters passed on to methods
Examples
df1 <- tidytable(x = 1:3)
df2 <- tidytable(x = c(2, 3, 3), y = c("a", "b", "c"))
Description
Create a tidytable from a list
Usage
new_tidytable(x = list())
Arguments
x A named list of equal-length vectors. The lengths are not checked; it is the
responsibility of the caller to make sure they are equal.
42 pick
Examples
l <- list(x = 1:3, y = c("a", "a", "b"))
new_tidytable(l)
Description
Usage
Arguments
Examples
x <- sample(1:10, 1e5, rep = TRUE)
n_distinct(x)
Description
Select a subset of columns from within functions like mutate(), summarize(), or filter().
Usage
pick(...)
Arguments
Examples
df <- tidytable(
x = 1:3,
y = 4:6,
z = c("a", "a", "b")
)
df %>%
mutate(row_sum = rowSums(pick(x, y)))
Description
pivot_longer() "lengthens" the data, increasing the number of rows and decreasing the number
of columns.
Usage
pivot_longer(
.df,
cols = everything(),
names_to = "name",
values_to = "value",
names_prefix = NULL,
names_sep = NULL,
names_pattern = NULL,
names_ptypes = NULL,
names_transform = NULL,
names_repair = "check_unique",
values_drop_na = FALSE,
values_ptypes = NULL,
values_transform = NULL,
fast_pivot = FALSE,
...
)
Arguments
.df A data.table or data.frame
cols Columns to pivot. tidyselect compatible.
names_to Name of the new "names" column. Must be a string.
values_to Name of the new "values" column. Must be a string.
names_prefix Remove matching text from the start of selected columns using regex.
44 pivot_wider
names_sep If names_to contains multiple values, names_sep takes the same specification
as separate().
names_pattern If names_to contains multiple values, names_pattern takes the same specifica-
tion as extract(), a regular expression containing matching groups.
names_ptypes, values_ptypes
A list of column name-prototype pairs. See “?vctrs::‘theory-faq-coercion“‘ for
more info on vctrs coercion.
names_transform, values_transform
A list of column name-function pairs. Use these arguments if you need to change
the types of specific columns.
names_repair Treatment of duplicate names. See ?vctrs::vec_as_names for options/details.
values_drop_na If TRUE, rows will be dropped that contain NAs.
fast_pivot experimental: Fast pivoting. If TRUE, the names_to column will be returned as
a factor, otherwise it will be a character column. Defaults to FALSE to match
tidyverse semantics.
... Additional arguments to passed on to methods.
Examples
df <- data.table(
x = 1:3,
y = 4:6,
z = c("a", "b", "c")
)
df %>%
pivot_longer(cols = c(x, y))
df %>%
pivot_longer(cols = -z, names_to = "stuff", values_to = "things")
Description
"Widens" data, increasing the number of columns and decreasing the number of rows.
Usage
pivot_wider(
.df,
names_from = name,
values_from = value,
id_cols = NULL,
names_sep = "_",
pivot_wider 45
names_prefix = "",
names_glue = NULL,
names_sort = FALSE,
names_repair = "unique",
values_fill = NULL,
values_fn = NULL,
unused_fn = NULL
)
Arguments
.df A data.frame or data.table
names_from A pair of arguments describing which column (or columns) to get the name of
the output column name_from, and which column (or columns) to get the cell
values from values_from). tidyselect compatible.
values_from A pair of arguments describing which column (or columns) to get the name of
the output column name_from, and which column (or columns) to get the cell
values from values_from. tidyselect compatible.
id_cols A set of columns that uniquely identifies each observation. Defaults to all
columns in the data table except for the columns specified in names_from and
values_from. Typically used when you have additional variables that is directly
related. tidyselect compatible.
names_sep the separator between the names of the columns
names_prefix prefix to add to the names of the new columns
names_glue Instead of using names_sep and names_prefix, you can supply a glue specifi-
cation that uses the names_from columns (and special .value) to create custom
column names
names_sort Should the resulting new columns be sorted.
names_repair Treatment of duplicate names. See ?vctrs::vec_as_names for options/details.
values_fill If values are missing, what value should be filled in
values_fn Should the data be aggregated before casting? If the formula doesn’t identify
a single observation for each cell, then aggregation defaults to length with a
message.
unused_fn Aggregation function to be applied to unused columns. Default is to ignore
unused columns.
Examples
df <- tidytable(
id = 1,
names = c("a", "b", "c"),
vals = 1:3
)
df %>%
pivot_wider(names_from = names, values_from = vals)
46 pull
df %>%
pivot_wider(
names_from = names, values_from = vals, names_prefix = "new_"
)
Description
Pull a single variable from a data.table as a vector.
Usage
pull(.df, var = -1, name = NULL)
Arguments
.df A data.frame or data.table
var The column to pull from the data.table as:
• a variable name
• a positive integer giving the column position
• a negative integer giving the column position counting from the right
name Optional - specifies the column to be used as names for the vector.
Examples
df <- data.table(
x = 1:3,
y = 1:3
)
Description
Reframe a data frame. Note this is a simple alias for summarize() that always returns an ungrouped
tidytable.
Usage
reframe(.df, ..., .by = NULL)
Arguments
.df A data.frame or data.table
... Aggregations to perform
.by Columns to group by
Examples
mtcars %>%
reframe(qs = quantile(disp, c(0.25, 0.75)),
prob = c(0.25, 0.75),
.by = cyl)
Description
Move a column or columns to a new position
Usage
relocate(.df, ..., .before = NULL, .after = NULL)
Arguments
.df A data.frame or data.table
... A selection of columns to move. tidyselect compatible.
.before Column to move selection before
.after Column to move selection after
48 rename
Examples
df <- data.table(
a = 1:3,
b = 1:3,
c = c("a", "a", "b"),
d = c("a", "a", "b")
)
df %>%
relocate(c, .before = b)
df %>%
relocate(a, b, .after = c)
df %>%
relocate(where(is.numeric), .after = c)
Description
Usage
rename(.df, ...)
Arguments
Examples
df %>%
rename(new_x = x,
new_y = y)
rename_with 49
Description
Rename multiple columns with the same transformation
Usage
rename_with(.df, .fn = NULL, .cols = everything(), ...)
Arguments
.df A data.table or data.frame
.fn Function to transform the names with.
.cols Columns to rename. Defaults to all columns. tidyselect compatible.
... Other parameters to pass to the function
Examples
df <- data.table(
x = 1,
y = 2,
double_x = 2,
double_y = 4
)
df %>%
rename_with(toupper)
df %>%
rename_with(~ toupper(.x))
df %>%
rename_with(~ toupper(.x), .cols = c(x, double_x))
Description
Replace NAs with specified values
Usage
replace_na(.x, replace)
50 rowwise
Arguments
.x A data.frame/data.table or a vector
replace If .x is a data frame, a list() of replacement values for specified columns. If
.x is a vector, a single replacement value.
Examples
df <- data.table(
x = c(1, 2, NA),
y = c(NA, 1, 2)
)
Description
Convert to a rowwise tidytable.
Usage
rowwise(.df)
Arguments
.df A data.frame or data.table
Examples
df <- tidytable(x = 1:3, y = 1:3 * 2, z = 1:3 * 3)
Description
Ranking functions:
Usage
row_number(x)
min_rank(x)
dense_rank(x)
percent_rank(x)
cume_dist(x)
Arguments
x A vector to rank
Examples
df %>%
mutate(row = row_number())
52 select
Description
Usage
select(.df, ...)
Arguments
Examples
df <- data.table(
x1 = 1:3,
x2 = 1:3,
y = c("a", "b", "c"),
z = c("a", "b", "c")
)
df %>%
select(x1, y)
df %>%
select(x1:y)
df %>%
select(-y, -z)
df %>%
select(starts_with("x"), z)
df %>%
select(where(is.character), x1)
df %>%
select(new = x1, y)
separate 53
Description
Superseded
separate() has been superseded by separate_wider_delim().
Separates a single column into multiple columns using a user supplied separator or regex.
If a separator is not supplied one will be automatically detected.
Note: Using automatic detection or regex will be slower than simple separators such as "," or ".".
Usage
separate(
.df,
col,
into,
sep = "[^[:alnum:]]+",
remove = TRUE,
convert = FALSE,
...
)
Arguments
.df A data frame
col The column to split into multiple columns
into New column names to split into. A character vector. Use NA to omit the variable
in the output.
sep Separator to split on. Can be specified or detected automatically
remove If TRUE, remove the input column from the output data.table
convert TRUE calls type.convert() with as.is = TRUE on new columns
... Arguments passed on to methods
Examples
df <- data.table(x = c("a", "a.b", "a.b", NA))
Description
If a column contains observations with multiple delimited values, separate them each into their own
row.
Usage
separate_longer_delim(.df, cols, delim, ...)
Arguments
.df A data.frame or data.table
cols Columns to separate
delim Separator delimiting collapsed values
... These dots are for future extensions and must be empty.
Examples
df <- data.table(
x = 1:3,
y = c("a", "d,e,f", "g,h"),
z = c("1", "2,3,4", "5,6")
)
df %>%
separate_longer_delim(c(y, z), ",")
Description
Superseded
separate_rows() has been superseded by separate_longer_delim().
If a column contains observations with multiple delimited values, separate them each into their own
row.
Usage
separate_rows(.df, ..., sep = "[^[:alnum:].]+", convert = FALSE)
separate_wider_delim 55
Arguments
.df A data.frame or data.table
... Columns to separate across multiple rows. tidyselect compatible
sep Separator delimiting collapsed values
convert If TRUE, runs type.convert() on the resulting column. Useful if the resulting
column should be type integer/double.
Examples
df <- data.table(
x = 1:3,
y = c("a", "d,e,f", "g,h"),
z = c("1", "2,3,4", "5,6")
)
separate_rows(df, y, z)
Description
Separates a single column into multiple columns
Usage
separate_wider_delim(
.df,
cols,
delim,
...,
names = NULL,
names_sep = NULL,
names_repair = "check_unique",
too_few = c("align_start", "error"),
too_many = c("drop", "error"),
cols_remove = TRUE
)
Arguments
.df A data frame
cols Columns to separate
delim Delimiter to separate on
56 separate_wider_regex
... These dots are for future extensions and must be empty.
names New column names to separate into
names_sep Names separator
names_repair Treatment of duplicate names. See ?vctrs::vec_as_names for options/details.
too_few What to do when too few column names are supplied
too_many What to do when too many column names are supplied
cols_remove Should old columns be removed
Examples
df <- tidytable(x = c("a", "a_b", "a_b", NA))
df %>%
separate_wider_delim(x, delim = "_", names = c("left", "right"))
df %>%
separate_wider_delim(x, delim = "_", names_sep = "")
separate_wider_regex Separate a character column into multiple columns using regex pat-
terns
Description
Separate a character column into multiple columns using regex patterns
Usage
separate_wider_regex(
.df,
cols,
patterns,
...,
names_sep = NULL,
names_repair = "check_unique",
too_few = "error",
cols_remove = TRUE
)
Arguments
.df A data frame
cols Columns to separate
patterns patterns
... These dots are for future extensions and must be empty.
slice_head 57
Examples
df <- tidytable(id = 1:3, x = c("m-123", "f-455", "f-123"))
df %>%
separate_wider_regex(x, c(gender = ".", ".", unit = "\\d+"))
Description
Choose rows in a data.table. Grouped data.tables grab rows within each group.
Usage
slice_head(.df, n = 5, ..., .by = NULL, by = NULL)
slice_sample(
.df,
n,
prop,
weight_by = NULL,
replace = FALSE,
.by = NULL,
by = NULL
)
Arguments
.df A data.frame or data.table
n Number of rows to grab
... Integer row values
58 summarize
Examples
df <- data.table(
x = 1:4,
y = 5:8,
z = c("a", "a", "a", "b")
)
df %>%
slice(1:3)
df %>%
slice(1, 3)
df %>%
slice(1:2, .by = z)
df %>%
slice_head(1, .by = z)
df %>%
slice_tail(1, .by = z)
df %>%
slice_max(order_by = x, .by = z)
df %>%
slice_min(order_by = y, .by = z)
Description
Aggregate data using summary statistics such as mean or median. Can be calculated by group.
summarize 59
Usage
summarize(
.df,
...,
.by = NULL,
.sort = TRUE,
.groups = "drop_last",
.unpack = FALSE
)
summarise(
.df,
...,
.by = NULL,
.sort = TRUE,
.groups = "drop_last",
.unpack = FALSE
)
Arguments
.df A data.frame or data.table
... Aggregations to perform
.by Columns to group by.
• A single column can be passed with .by = d.
• Multiple columns can be passed with .by = c(c, d)
• tidyselect can be used:
– Single predicate: .by = where(is.character)
– Multiple predicates: .by = c(where(is.character), where(is.factor))
– A combination of predicates and column names: .by = c(where(is.character),
b)
.sort experimental: Default TRUE. If FALSE the original order of the grouping vari-
ables will be preserved.
.groups Grouping structure of the result
• "drop_last": Drop the last level of grouping
• "drop": Drop all groups
• "keep": Keep all groups
.unpack experimental: Default FALSE. Should unnamed data frame inputs be unpacked.
The user must opt in to this option as it can lead to a reduction in performance.
Examples
df <- data.table(
a = 1:3,
b = 4:6,
60 top_n
df %>%
summarize(avg_a = mean(a),
max_b = max(b),
.by = c)
df %>%
summarize(avg_a = mean(a),
.by = c(c, d))
Description
Constructs a data.table, but one with nice printing features.
Usage
tidytable(..., .name_repair = "unique")
Arguments
... A set of name-value pairs
.name_repair Treatment of duplicate names. See ?vctrs::vec_as_names for options/details.
Examples
tidytable(x = 1:3, y = c("a", "a", "b"))
Description
Select the top or bottom entries in each group, ordered by wt.
Usage
top_n(.df, n = 5, wt = NULL, .by = NULL)
transmute 61
Arguments
.df A data.frame or data.table
n Number of rows to return
wt Optional. The variable to use for ordering. If NULL uses the last column in the
data.table.
.by Columns to group by
Examples
df <- data.table(
x = 1:5,
y = 6:10,
z = c(rep("a", 3), rep("b", 2))
)
df %>%
top_n(2, wt = y)
df %>%
top_n(2, wt = y, .by = z)
Description
Unlike mutate(), transmute() keeps only the variables that you create
Usage
transmute(.df, ..., .by = NULL)
Arguments
.df A data.frame or data.table
... Columns to create/modify
.by Columns to group by
Examples
df <- data.table(
a = 1:3,
b = 4:6,
c = c("a", "a", "b")
)
df %>%
transmute(double_a = a * 2)
62 uncount
Description
Create a tidytable using a rowwise setup.
Usage
tribble(...)
Arguments
... Column names as formulas, values below. See example.
Examples
tribble(
~ x, ~ y,
"a", 1,
"b", 2,
"c", 3
)
Description
Uncount a data.table
Usage
uncount(.df, weights, .remove = TRUE, .id = NULL)
Arguments
.df A data.frame or data.table
weights A column containing the weights to uncount by
.remove If TRUE removes the selected weights column
.id A string name for a new column containing a unique identifier for the newly
uncounted rows.
unite 63
Examples
df <- data.table(x = c("a", "b"), n = c(1, 2))
uncount(df, n)
Description
Convenience function to paste together multiple columns into one.
Usage
unite(.df, col = ".united", ..., sep = "_", remove = TRUE, na.rm = FALSE)
Arguments
.df A data.frame or data.table
col Name of the new column, as a string.
... Selection of columns. If empty all variables are selected. tidyselect compati-
ble.
sep Separator to use between values
remove If TRUE, removes input columns from the data.table.
na.rm If TRUE, NA values will be not be part of the concatenation
Examples
df <- tidytable(
a = c("a", "a", "a"),
b = c("b", "b", "b"),
c = c("c", "c", NA)
)
df %>%
unite("new_col", b, c)
df %>%
unite("new_col", where(is.character))
df %>%
unite("new_col", b, c, remove = FALSE)
df %>%
unite("new_col", b, c, na.rm = TRUE)
64 unnest
df %>%
unite()
Description
Unnest list-columns.
Usage
unnest(
.df,
...,
keep_empty = FALSE,
.drop = TRUE,
names_sep = NULL,
names_repair = "unique"
)
Arguments
.df A data.table
... Columns to unnest If empty, unnests all list columns. tidyselect compatible.
keep_empty Return NA for any NULL elements of the list column
.drop Should list columns that were not unnested be dropped
names_sep If NULL, the default, the inner column names will become the new outer column
names.
If a string, the name of the outer column will be appended to the beginning of
the inner column names, with names_sep used as a separator.
names_repair Treatment of duplicate names. See ?vctrs::vec_as_names for options/details.
Examples
df1 <- tidytable(x = 1:3, y = 1:3)
df2 <- tidytable(x = 1:2, y = 1:2)
nested_df <-
data.table(
a = c("a", "b"),
frame_list = list(df1, df2),
vec_list = list(4:6, 7:8)
)
nested_df %>%
unnest(frame_list)
unnest_longer 65
nested_df %>%
unnest(frame_list, names_sep = "_")
nested_df %>%
unnest(frame_list, vec_list)
Description
Turns each element of a list-column into a row.
Usage
unnest_longer(
.df,
col,
values_to = NULL,
indices_to = NULL,
indices_include = NULL,
keep_empty = FALSE,
names_repair = "check_unique",
simplify = NULL,
ptype = NULL,
transform = NULL
)
Arguments
.df A data.table or data.frame
col Column to unnest
values_to Name of column to store values
indices_to Name of column to store indices
indices_include
Should an index column be included? Defaults to TRUE when col has inner
names.
keep_empty Return NA for any NULL elements of the list column
names_repair Treatment of duplicate names. See ?vctrs::vec_as_names for options/details.
simplify Currently not supported. Errors if not NULL.
ptype Optionally a named list of ptypes declaring the desired output type of each com-
ponent.
transform Optionally a named list of transformation functions applied to each component.
66 unnest_wider
Examples
df <- tidytable(
x = 1:3,
y = list(0, 1:3, 4:5)
)
df %>% unnest_longer(y)
Description
Unnest a list-column of vectors into a wide data frame
Usage
unnest_wider(
.df,
col,
names_sep = NULL,
simplify = NULL,
names_repair = "check_unique",
ptype = NULL,
transform = NULL
)
Arguments
.df A data.table or data.frame
col Column to unnest
names_sep If NULL, the default, the names will be left as they are. If a string, the inner and
outer names will be pasted together with names_sep as the separator.
simplify Currently not supported. Errors if not NULL.
names_repair Treatment of duplicate names. See ?vctrs::vec_as_names for options/details.
ptype Optionally a named list of ptypes declaring the desired output type of each com-
ponent.
transform Optionally a named list of transformation functions applied to each component.
Examples
df <- tidytable(
x = 1:3,
y = list(0, 1:3, 4:5)
)
%in% 67
Description
Check whether values in a vector are in or not in another vector.
Built using data.table::'%chin%' and vctrs::vec_in() for performance.
Usage
x %in% y
x %notin% y
Arguments
x A vector of values to check if they exist in y
y A vector of values to check if x values exist in
Details
Falls back to base::'%in%' when x and y don’t share a common type. This means that the be-
haviour of base::'%in%' is preserved (e.g. "1" %in% c(1, 2) is TRUE) but loses the speedup pro-
vided by vctrs::vec_in().
Examples
df <- tidytable(x = 1:4, y = 1:4)
df %>%
filter(x %in% c(2, 4))
df %>%
filter(x %notin% c(2, 4))
Index
68
INDEX 69
reframe, 47
relocate, 47
rename, 48
rename_with, 49
replace_na, 49
right_join (left_join), 33
row_number, 51
rowwise, 50
select, 52
semi_join (left_join), 33
separate, 53
separate_longer_delim, 54
separate_rows, 54
separate_wider_delim, 55
separate_wider_regex, 56
slice (slice_head), 57