Analogous function for pivot_longer
in tidyr.
longer_dt( data, ..., gathered_name = "variable", gathered_value = "value", group_to_keep = NULL, negate = FALSE, na.rm = FALSE )
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 |
na.rm | If |
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.400043517 -1.1073988 1.87261768 #> 2 2009-01-02 0.255317055 1.2579641 1.45180502 #> 3 2009-01-03 -2.437263611 4.1300498 -5.21817418 #> 4 2009-01-04 -0.005571287 -3.2619788 2.95110529 #> 5 2009-01-05 0.621552721 1.0248539 7.55401972 #> 6 2009-01-06 1.148411606 -3.7260230 -0.38978042 #> 7 2009-01-07 -1.821817661 -1.0440250 -3.74338941 #> 8 2009-01-08 -0.247325302 -0.1052038 -0.06380125 #> 9 2009-01-09 -0.244199607 1.0859927 -3.30715581 #> 10 2009-01-10 -0.282705449 -1.8281497 -6.04959861stocks %>% longer_dt(time)#> time variable value #> 1: 2009-01-01 X -1.400043517 #> 2: 2009-01-02 X 0.255317055 #> 3: 2009-01-03 X -2.437263611 #> 4: 2009-01-04 X -0.005571287 #> 5: 2009-01-05 X 0.621552721 #> 6: 2009-01-06 X 1.148411606 #> 7: 2009-01-07 X -1.821817661 #> 8: 2009-01-08 X -0.247325302 #> 9: 2009-01-09 X -0.244199607 #> 10: 2009-01-10 X -0.282705449 #> 11: 2009-01-01 Y -1.107398767 #> 12: 2009-01-02 Y 1.257964084 #> 13: 2009-01-03 Y 4.130049791 #> 14: 2009-01-04 Y -3.261978804 #> 15: 2009-01-05 Y 1.024853900 #> 16: 2009-01-06 Y -3.726022984 #> 17: 2009-01-07 Y -1.044025029 #> 18: 2009-01-08 Y -0.105203820 #> 19: 2009-01-09 Y 1.085992685 #> 20: 2009-01-10 Y -1.828149655 #> 21: 2009-01-01 Z 1.872617682 #> 22: 2009-01-02 Z 1.451805023 #> 23: 2009-01-03 Z -5.218174180 #> 24: 2009-01-04 Z 2.951105285 #> 25: 2009-01-05 Z 7.554019717 #> 26: 2009-01-06 Z -0.389780418 #> 27: 2009-01-07 Z -3.743389414 #> 28: 2009-01-08 Z -0.063801245 #> 29: 2009-01-09 Z -3.307155815 #> 30: 2009-01-10 Z -6.049598605 #> time variable valuestocks %>% longer_dt("ti")#> time variable value #> 1: 2009-01-01 X -1.400043517 #> 2: 2009-01-02 X 0.255317055 #> 3: 2009-01-03 X -2.437263611 #> 4: 2009-01-04 X -0.005571287 #> 5: 2009-01-05 X 0.621552721 #> 6: 2009-01-06 X 1.148411606 #> 7: 2009-01-07 X -1.821817661 #> 8: 2009-01-08 X -0.247325302 #> 9: 2009-01-09 X -0.244199607 #> 10: 2009-01-10 X -0.282705449 #> 11: 2009-01-01 Y -1.107398767 #> 12: 2009-01-02 Y 1.257964084 #> 13: 2009-01-03 Y 4.130049791 #> 14: 2009-01-04 Y -3.261978804 #> 15: 2009-01-05 Y 1.024853900 #> 16: 2009-01-06 Y -3.726022984 #> 17: 2009-01-07 Y -1.044025029 #> 18: 2009-01-08 Y -0.105203820 #> 19: 2009-01-09 Y 1.085992685 #> 20: 2009-01-10 Y -1.828149655 #> 21: 2009-01-01 Z 1.872617682 #> 22: 2009-01-02 Z 1.451805023 #> 23: 2009-01-03 Z -5.218174180 #> 24: 2009-01-04 Z 2.951105285 #> 25: 2009-01-05 Z 7.554019717 #> 26: 2009-01-06 Z -0.389780418 #> 27: 2009-01-07 Z -3.743389414 #> 28: 2009-01-08 Z -0.063801245 #> 29: 2009-01-09 Z -3.307155815 #> 30: 2009-01-10 Z -6.049598605 #> time variable value# Example 2: # \donttest{ library(tidyr) library(tidydt) billboard %>% longer_dt( "wk", gathered_name = "week", gathered_value = "rank", na.rm = TRUE,negate = 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 #> 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, gathered_name = "week", gathered_value = "rank", na.rm = TRUE,negate = 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 #> 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( group_to_keep = 1:3, gathered_name = "week", gathered_value = "rank", na.rm = TRUE,negate = 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 #> 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# }