Learn R Programming

tidyfst (version 0.7.7)

longer_dt: Pivot data from wide to long

Description

Analogous function for pivot_longer in tidyr.

Usage

longer_dt(
  data,
  ...,
  gathered_name = "variable",
  gathered_value = "value",
  group_to_keep = NULL,
  negate = FALSE,
  na.rm = FALSE
)

Arguments

data

A data.frame

...

Pattern for unchanged group or unquoted names. Pattern can accept regular expression to match column names. If set `negate = TRUE`, return non-matching columns.

gathered_name

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

gathered_value

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

group_to_keep

Group to keep, namely vector of unchanged variables. Can be integer (corresponding id column numbers) or character (id column names) vector.

negate

If TRUE, return non-matching elements.

na.rm

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

Value

A data.table

See Also

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",
    gathered_name = "week",
    gathered_value = "rank",
    na.rm = TRUE,negate = TRUE
  )

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

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

Run the code above in your browser using DataLab