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

longer

Pivot data from wide to long


Description

Turning a wide table to its longer form. It takes multiple columns and collapses into key-value pairs.

Usage

longer_dt(.data, ..., name = "name", 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.

name

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

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

Examples

## 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:


  library(tidyr)

  billboard %>%
    longer_dt(
      -"wk",
      name = "week",
      value = "rank",
      na.rm = TRUE
    )

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

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

tidyfst

Tidy Verbs for Fast Data Manipulation

v0.9.9
MIT + file LICENSE
Authors
Tian-Yuan Huang [aut, cre] (<https://orcid.org/0000-0002-3591-4203>)
Initial release

We don't support your browser anymore

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