longer_dt: Pivot data from wide to long

Description Usage Arguments Value See Also Examples

View source: R/long_wide.R View source: R/longer_dt.R

Description

Analogous function for pivot_longer in tidyr.

Usage

1
2
3
4
5
6
7
8
9
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

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
## 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)
library(tidydt)

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
   )

hope-data-science/tidydt documentation built on Feb. 21, 2020, 10:25 a.m.