Learn R Programming

tidyfst (version 0.8.8)

longer_dt: Pivot data from wide to long

Description

Analogous function for pivot_longer in tidyr.

Usage

longer_dt(
  data,
  ...,
  gathered_name = "name",
  gathered_value = "value",
  na.rm = FALSE
)

Arguments

data

A data.frame

...

Pattern for unchanged group or unquoted names. Pattern can accept regular expression to match column names. It can recieve what select_dt recieves.

gathered_name

name for the measured variable names column. The default name is 'name'.

gathered_value

name for the molten data values column(s). The default name is 'value'.

na.rm

If TRUE, NA values will be removed from the molten data.

Value

A data.table

See Also

wider_dt, melt, pivot_longer

Examples

Run this code
# NOT RUN {
## Example 1:
stocks = data.frame(
  time = as.Date('2009-01-01') + 0:9,
  X = rnorm(10, 0, 1),
  Y = rnorm(10, 0, 2),
  Z = rnorm(10, 0, 4)
)

stocks

stocks %>%
  longer_dt(time)

stocks %>%
  longer_dt("ti")

# Example 2:

# }
# NOT RUN {
  library(tidyr)

  billboard %>%
    longer_dt(
      "wk",negate = TRUE,
      gathered_name = "week",
      gathered_value = "rank",
      na.rm = TRUE
    )

  # or use:
  billboard %>%
    longer_dt(
      artist,track,date.entered,
      gathered_name = "week",
      gathered_value = "rank",
      na.rm = TRUE
    )

  # or use:
  billboard %>%
    longer_dt(
      1:3,
      gathered_name = "week",
      gathered_value = "rank",
      na.rm = TRUE
    )
# }

Run the code above in your browser using DataLab