pivot_longer: Pivot data from wide to long

View source: R/pivot_longer.R

pivot_longerR Documentation

Pivot data from wide to long

Description

pivot_longer() "lengthens" data, increasing the number of rows and decreasing the number of columns. The inverse transformation is pivot_wider().

Usage

pivot_longer(
  data,
  cols,
  names_to = "name",
  names_prefix = NULL,
  names_sep = NULL,
  names_pattern = NULL,
  values_to = "value",
  values_drop_na = FALSE,
  ...
)

Arguments

data

data.frame. The data to pivot.

cols

<poor-select>. Columns to pivot into longer format.

names_to

character(n). The name of the new column(s) that will contain the column names.

names_prefix

character(1). A regular expression used to remove matching text from the start of each variable name.

names_sep, names_pattern

character(1). If names_to contains multiple values, this argument controls how the column name is broken up. names_pattern takes a regular expression containing matching groups (⁠()⁠).

values_to

character(n). The name of the new column(s) that will contain the values of the pivoted variables.

values_drop_na

logical(1). If TRUE, will drop rows that contain only NA in the values_to column. This effectively converts explicit missing values to implicit missing values, and should generally be used only when missing values in data were created by its structure.

...

Additional arguments passed on to methods.

Value

A data.frame.

Examples

wide_data <- data.frame(replicate(5, rnorm(10)))
# Customizing the names
pivot_longer(
  data = wide_data,
  cols = c(1, 2),
  names_to = "Column",
  values_to = "Numbers"
)


poorman documentation built on Nov. 2, 2023, 5:27 p.m.