Turning a wide table to its longer form. It takes multiple columns and collapses into key-value pairs.
longer_dt(.data, ..., name = "name", value = "value", na.rm = FALSE)
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 for the measured variable names column. The default name is 'name'.
Name for the molten data values column(s). The default name is 'value'.
If TRUE
, NA
values will be removed from the molten data.
A data.table
## 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
#> time X Y Z
#> 1 2009-01-01 -1.5171329 -2.58389994 3.2247414
#> 2 2009-01-02 0.1879811 0.09984914 2.9869549
#> 3 2009-01-03 -0.7008255 1.91396165 0.4223763
#> 4 2009-01-04 -0.4731759 1.32351293 5.4705240
#> 5 2009-01-05 -0.4651643 0.03160291 4.4782044
#> 6 2009-01-06 0.4315827 1.70596009 -0.8897295
#> 7 2009-01-07 0.2985233 1.89657625 3.6283370
#> 8 2009-01-08 -0.6874585 -1.87925647 3.6727750
#> 9 2009-01-09 -0.7381848 0.72525958 -8.4367991
#> 10 2009-01-10 0.7062822 -2.57758846 4.1861255
stocks %>%
longer_dt(time)
#> time name value
#> <Date> <fctr> <num>
#> 1: 2009-01-01 X -1.51713292
#> 2: 2009-01-02 X 0.18798114
#> 3: 2009-01-03 X -0.70082548
#> 4: 2009-01-04 X -0.47317586
#> 5: 2009-01-05 X -0.46516427
#> 6: 2009-01-06 X 0.43158267
#> 7: 2009-01-07 X 0.29852330
#> 8: 2009-01-08 X -0.68745848
#> 9: 2009-01-09 X -0.73818478
#> 10: 2009-01-10 X 0.70628216
#> 11: 2009-01-01 Y -2.58389994
#> 12: 2009-01-02 Y 0.09984914
#> 13: 2009-01-03 Y 1.91396165
#> 14: 2009-01-04 Y 1.32351293
#> 15: 2009-01-05 Y 0.03160291
#> 16: 2009-01-06 Y 1.70596009
#> 17: 2009-01-07 Y 1.89657625
#> 18: 2009-01-08 Y -1.87925647
#> 19: 2009-01-09 Y 0.72525958
#> 20: 2009-01-10 Y -2.57758846
#> 21: 2009-01-01 Z 3.22474136
#> 22: 2009-01-02 Z 2.98695488
#> 23: 2009-01-03 Z 0.42237631
#> 24: 2009-01-04 Z 5.47052395
#> 25: 2009-01-05 Z 4.47820437
#> 26: 2009-01-06 Z -0.88972954
#> 27: 2009-01-07 Z 3.62833699
#> 28: 2009-01-08 Z 3.67277502
#> 29: 2009-01-09 Z -8.43679908
#> 30: 2009-01-10 Z 4.18612551
#> time name value
stocks %>%
longer_dt("ti")
#> time name value
#> <Date> <fctr> <num>
#> 1: 2009-01-01 X -1.51713292
#> 2: 2009-01-02 X 0.18798114
#> 3: 2009-01-03 X -0.70082548
#> 4: 2009-01-04 X -0.47317586
#> 5: 2009-01-05 X -0.46516427
#> 6: 2009-01-06 X 0.43158267
#> 7: 2009-01-07 X 0.29852330
#> 8: 2009-01-08 X -0.68745848
#> 9: 2009-01-09 X -0.73818478
#> 10: 2009-01-10 X 0.70628216
#> 11: 2009-01-01 Y -2.58389994
#> 12: 2009-01-02 Y 0.09984914
#> 13: 2009-01-03 Y 1.91396165
#> 14: 2009-01-04 Y 1.32351293
#> 15: 2009-01-05 Y 0.03160291
#> 16: 2009-01-06 Y 1.70596009
#> 17: 2009-01-07 Y 1.89657625
#> 18: 2009-01-08 Y -1.87925647
#> 19: 2009-01-09 Y 0.72525958
#> 20: 2009-01-10 Y -2.57758846
#> 21: 2009-01-01 Z 3.22474136
#> 22: 2009-01-02 Z 2.98695488
#> 23: 2009-01-03 Z 0.42237631
#> 24: 2009-01-04 Z 5.47052395
#> 25: 2009-01-05 Z 4.47820437
#> 26: 2009-01-06 Z -0.88972954
#> 27: 2009-01-07 Z 3.62833699
#> 28: 2009-01-08 Z 3.67277502
#> 29: 2009-01-09 Z -8.43679908
#> 30: 2009-01-10 Z 4.18612551
#> time name value
# Example 2:
# \donttest{
library(tidyr)
billboard %>%
longer_dt(
-"wk",
name = "week",
value = "rank",
na.rm = TRUE
)
#> Warning: 'measure.vars' [wk1, wk2, wk3, wk4, ...] are not all of the same type. By order of hierarchy, the molten data value column will be of type 'double'. All measure variables not of type 'double' will be coerced too. Check DETAILS in ?melt.data.table for more on coercion.
#> artist track date.entered week rank
#> <char> <char> <Date> <fctr> <num>
#> 1: 2 Pac Baby Don't Cry (Keep... 2000-02-26 wk1 87
#> 2: 2Ge+her The Hardest Part Of ... 2000-09-02 wk1 91
#> 3: 3 Doors Down Kryptonite 2000-04-08 wk1 81
#> 4: 3 Doors Down Loser 2000-10-21 wk1 76
#> 5: 504 Boyz Wobble Wobble 2000-04-15 wk1 57
#> ---
#> 5303: Creed Higher 1999-09-11 wk63 50
#> 5304: Lonestar Amazed 1999-06-05 wk63 45
#> 5305: Creed Higher 1999-09-11 wk64 50
#> 5306: Lonestar Amazed 1999-06-05 wk64 50
#> 5307: Creed Higher 1999-09-11 wk65 49
# or use:
billboard %>%
longer_dt(
artist,track,date.entered,
name = "week",
value = "rank",
na.rm = TRUE
)
#> Warning: 'measure.vars' [wk1, wk2, wk3, wk4, ...] are not all of the same type. By order of hierarchy, the molten data value column will be of type 'double'. All measure variables not of type 'double' will be coerced too. Check DETAILS in ?melt.data.table for more on coercion.
#> artist track date.entered week rank
#> <char> <char> <Date> <fctr> <num>
#> 1: 2 Pac Baby Don't Cry (Keep... 2000-02-26 wk1 87
#> 2: 2Ge+her The Hardest Part Of ... 2000-09-02 wk1 91
#> 3: 3 Doors Down Kryptonite 2000-04-08 wk1 81
#> 4: 3 Doors Down Loser 2000-10-21 wk1 76
#> 5: 504 Boyz Wobble Wobble 2000-04-15 wk1 57
#> ---
#> 5303: Creed Higher 1999-09-11 wk63 50
#> 5304: Lonestar Amazed 1999-06-05 wk63 45
#> 5305: Creed Higher 1999-09-11 wk64 50
#> 5306: Lonestar Amazed 1999-06-05 wk64 50
#> 5307: Creed Higher 1999-09-11 wk65 49
# or use:
billboard %>%
longer_dt(
1:3,
name = "week",
value = "rank",
na.rm = TRUE
)
#> Warning: 'measure.vars' [wk1, wk2, wk3, wk4, ...] are not all of the same type. By order of hierarchy, the molten data value column will be of type 'double'. All measure variables not of type 'double' will be coerced too. Check DETAILS in ?melt.data.table for more on coercion.
#> artist track date.entered week rank
#> <char> <char> <Date> <fctr> <num>
#> 1: 2 Pac Baby Don't Cry (Keep... 2000-02-26 wk1 87
#> 2: 2Ge+her The Hardest Part Of ... 2000-09-02 wk1 91
#> 3: 3 Doors Down Kryptonite 2000-04-08 wk1 81
#> 4: 3 Doors Down Loser 2000-10-21 wk1 76
#> 5: 504 Boyz Wobble Wobble 2000-04-15 wk1 57
#> ---
#> 5303: Creed Higher 1999-09-11 wk63 50
#> 5304: Lonestar Amazed 1999-06-05 wk63 45
#> 5305: Creed Higher 1999-09-11 wk64 50
#> 5306: Lonestar Amazed 1999-06-05 wk64 50
#> 5307: Creed Higher 1999-09-11 wk65 49
# }