Learn R Programming

tidytable (version 0.5.1)

pivot_longer.: Pivot data from wide to long

Description

pivot_wider.() "widens" data, increasing the number of columns and decreasing the number of rows. The inverse transformation is pivot_longer.(). Syntax based on the tidyr equivalents.

Usage

pivot_longer.(
  .df,
  cols = everything(),
  names_to = "name",
  values_to = "value",
  values_drop_na = FALSE,
  ...
)

dt_pivot_longer( .df, cols = everything(), names_to = "name", values_to = "value", values_drop_na = FALSE, ... )

Arguments

.df

The data table to pivot longer

cols

Vector of bare column names. Can add/drop columns. 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.

values_drop_na

If TRUE, rows will be dropped that contain NAs.

...

Additional arguments to pass to melt.df.table()

Examples

Run this code
# NOT RUN {
test_df <- data.table(
  x = c(1,2,3),
  y = c(4,5,6),
  z = c("a", "b", "c"))

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

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

Run the code above in your browser using DataLab