long_wide: Pivot data between long and wide

longerR Documentation

Pivot data between long and wide

Description

Fast table pivoting from long to wide and from wide to long. These functions are supported by dcast.data.table and melt.data.table from data.table.

Usage

longer(.data, ..., name = "name", value = "value", na.rm = FALSE)

wider(.data, ..., name, value = NULL, fun = NULL, fill = NA)

Arguments

.data

A data.table

...

Columns for unchanged group. Flexible, see examples.

name

Name for the measured variable names column.

value

Name for the data values column(s).

na.rm

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

fun

Should the data be aggregated before casting? Defaults to NULL, which uses length for aggregation. If a function is provided, with aggregated by this function.

fill

Value with which to fill missing cells. Default uses NA.

Value

A data.table

See Also

longer_dt,wider_dt

Examples


stocks <- data.table(
  time = as.Date('2009-01-01') + 0:9,
  X = rnorm(10, 0, 1),
  Y = rnorm(10, 0, 2),
  Z = rnorm(10, 0, 4)
)

stocks %>% longer(time)
stocks %>% longer(-(2:4)) # same
stocks %>% longer(-"X|Y|Z") # same
long_stocks = longer(stocks,"ti") # same as above except for assignment

long_stocks %>% wider(time,name = "name",value = "value")

# the unchanged group could be missed if all the rest will be used
long_stocks %>% wider(name = "name",value = "value")


tidyft documentation built on Sept. 30, 2024, 9:40 a.m.