long_wide: Convert data between wide and long forms

Description Usage Arguments Details Value See Also Examples

Description

Analogous function for dcast and melt in data.table

Usage

1
2
3
4
5
6
7
8
9
wider_dt(data, group, class_to_spread, value_to_spread = NULL, fill = NA)

longer_dt(
  data,
  group,
  gather_class = "class",
  gather_value = "value",
  na.rm = FALSE
)

Arguments

data

data.table

group

The unchanged group in the transformation

class_to_spread

The class of variable to spread

value_to_spread

The value of variable to spread. If missing, will guess automatically.

fill

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

gather_class

The column name of gathered class.

gather_value

The column name of gathered value.

na.rm

If TRUE, entries with NA values will be removed from the data.table.

Details

In the nest_dt, the data would be nested to a column named 'ndt', which is short for nested data.table.

Value

data.table

See Also

dcast,melt,dcast

pivot_longer,pivot_wider

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
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 %>%
  longer_dt(time) -> longer_stocks

longer_stocks

longer_stocks %>%
  wider_dt(time,class)

longer_stocks %>%
  mutate_dt(one = 1) %>%
  wider_dt(time,class,one)

hope-data-science/tidydt0 documentation built on Feb. 3, 2020, 12:04 a.m.