Become an expert in R — Interactive courses, Cheat Sheets, certificates and more!
Get Started for Free

pivot_longer..html

Pivot data from wide to long


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 = list(),
  names_transform = list(),
  names_repair = "check_unique",
  values_drop_na = FALSE,
  values_ptypes = list(),
  values_transform = list(),
  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.

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 specification 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

test_df <- data.table(
  x = 1:3,
  y = 4:6,
  z = c("a", "b", "c")
)

test_df %>%
  pivot_longer.(cols = c(x, y))

test_df %>%
  pivot_longer.(cols = -z, names_to = "stuff", values_to = "things")

tidytable

Tidy Interface to 'data.table'

v0.6.1
MIT + file LICENSE
Authors
Mark Fairbanks [aut, cre], Abdessabour Moutik [ctb], Matt Carlson [ctb], Ivan Leung [ctb], Ross Kennedy [ctb]
Initial release

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.