flat-map-df: Flat map and convert to a data frame

Description Usage Arguments Details Examples

Description

Usage

1
2
3
4
5
flat_map_dfr(.x, .f, ..., .ptype = NULL, .names_to = NULL,
  .name_repair = c("unique", "universal", "check_unique"))

flat_map_dfc(.x, .f, ..., .ptype = NULL, .size = NULL,
  .name_repair = c("unique", "universal", "check_unique", "minimal"))

Arguments

.x

A list or atomic vector.

.f

A function, formula, or vector (not necessarily atomic).

If a function, it is used as is.

If a formula, e.g. ~ .x + 2, it is converted to a function. There are three ways to refer to the arguments:

  • For a single argument function, use .

  • For a two argument function, use .x and .y

  • For more arguments, use ..1, ..2, ..3 etc

This syntax allows you to create very compact anonymous functions.

If character vector, numeric vector, or list, it is converted to an extractor function. Character vectors index by name and numeric vectors index by position; use a list to index by position and name at different levels. If a component is not present, the value of .default will be returned.

...

Data frames or vectors.

vec_rbind() ignores names unless .names_to is supplied. vec_cbind() creates packed data frame columns with named inputs.

NULL inputs are silently ignored. Empty (e.g. zero row) inputs will not appear in the output, but will affect the derived .ptype.

.ptype

If NULL, the default, the output type is determined by computing the common type across all elements of ....

Alternatively, you can supply .ptype to give the output known type. If getOption("vctrs.no_guessing") is TRUE you must supply this value: this is a convenient way to make production code demand fixed types.

.names_to

Optionally, the name of a column where the names of ... arguments are copied. These names are useful to identify which row comes from which input. If supplied and ... is not named, an integer column is used to identify the rows.

.name_repair

One of "unique", "universal", or "check_unique". See vec_as_names() for the meaning of these options.

With vec_rbind(), the repair function is applied to all inputs separately. This is because vec_rbind() needs to align their columns before binding the rows, and thus needs all inputs to have unique names. On the other hand, vec_cbind() applies the repair function after all inputs have been concatenated together in a final data frame. Hence vec_cbind() allows the more permissive minimal names repair.

.size

If, NULL, the default, will determine the number of rows in vec_cbind() output by using the standard recycling rules.

Alternatively, specify the desired number of rows, and any inputs of length 1 will be recycled appropriately.

Details

flat_map_dfr() and flat_map_dfc() try a bit harder to coerce the results of .f to a data frame than flat_map_vec() would. For example, if a result of .f is a 1d vector, for flat_map_dfr() it would be coerced into a 1 row data frame with as many columns as number of elements in the vector.

Examples

1
2
3
4
5
6
7
8
flat_map_dfr(1:5, ~data.frame(x = 1))

flat_map_dfc(1:5, ~data.frame(x = 1))

# `flat_map_dfr()` "tries harder" to coerce vectors to a data frame
try(flat_map_vec(1:5, ~.x, .ptype = data.frame(x = integer())))

flat_map_dfr(1:5, ~.x)

DavisVaughan/flatcat documentation built on Oct. 30, 2019, 5 p.m.