Analogous function for dcast
and melt
in data.table
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 )
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 |
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. |
data.table
In the nest_dt
, the data would be nested to a column named `ndt`,
which is short for nested data.table.
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) -> longer_stocks longer_stocks#> time class 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 class valuelonger_stocks %>% wider_dt(time,class)#> 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.04959861#> time X Y Z #> 1: 2009-01-01 1 1 1 #> 2: 2009-01-02 1 1 1 #> 3: 2009-01-03 1 1 1 #> 4: 2009-01-04 1 1 1 #> 5: 2009-01-05 1 1 1 #> 6: 2009-01-06 1 1 1 #> 7: 2009-01-07 1 1 1 #> 8: 2009-01-08 1 1 1 #> 9: 2009-01-09 1 1 1 #> 10: 2009-01-10 1 1 1