Skip to contents

Similar in spirit to tidyr::unnest(), the reverse of tf_nest(). The tf-method simply turns a single tfd or tfb vector into a "long" tibble().

Usage

tf_unnest(data, cols, arg, interpolate = TRUE, ...)

# S3 method for tf
tf_unnest(data, cols, arg, interpolate = TRUE, ...)

# S3 method for data.frame
tf_unnest(
  data,
  cols,
  arg,
  interpolate = TRUE,
  keep_empty = FALSE,
  ptype = NULL,
  names_sep = "_",
  names_repair = "check_unique",
  ...
)

Arguments

data

a data.frame or a tf-object

cols

<tidy-select> List-columns to unnest.

When selecting multiple columns, values from the same row will be recycled to their common size.

arg

optional values for the arg argument of tf_evaluate()

interpolate

return function values for arg-values not on original grid? Defaults to TRUE.

...

not used currently

keep_empty

By default, you get one row of output for each element of the list that you are unchopping/unnesting. This means that if there's a size-0 element (like NULL or an empty data frame or vector), then that entire row will be dropped from the output. If you want to preserve all rows, use keep_empty = TRUE to replace size-0 elements with a single row of missing values.

ptype

Optionally, a named list of column name-prototype pairs to coerce cols to, overriding the default that will be guessed from combining the individual values. Alternatively, a single empty ptype can be supplied, which will be applied to all cols.

names_sep

If NULL, the default, the outer names will come from the inner names. If a string, the outer names will be formed by pasting together the outer and the inner column names, separated by names_sep.

names_repair

Used to check that output data frame has valid names. Must be one of the following options:

  • "minimal": no name repair or checks, beyond basic existence,

  • "unique": make sure names are unique and not empty,

  • "check_unique": (the default), no name repair, but check they are unique,

  • "universal": make the names unique and syntactic

  • a function: apply custom name repair.

  • tidyr_legacy: use the name repair from tidyr 0.8.

  • a formula: a purrr-style anonymous function (see rlang::as_function())

See vctrs::vec_as_names() for more details on these terms and the strategies used to enforce them.

Value

a "long" data frame with tf-columns expanded into arg, value- columns.

Details

  • Caution -- uses slightly different defaults for names of unnested columns than tidyr::unnest().

  • For data.frames, make sure to have an ID column in your data before unnesting! If it does not include an ID column with a unique identifier for each row, you will not be able to match arg-value pairs to the different functions after unnesting.

See also

tf_evaluate.data.frame()

Other tidyfun data wrangling functions: tf_evaluate.data.frame(), tf_gather(), tf_nest(), tf_spread()