Analogous function for pivot_wider
in tidyr.
wider_dt(.data, ..., name, value = NULL, fun = NULL, fill = NA)
.data | A data.frame |
---|---|
... | Optional. The unchanged group in the transformation.
Could use integer vector, could receive what |
name | Chracter.One column name of class to spread |
value | Chracter.One column name of value to spread.
If |
fun | Should the data be aggregated before casting?
Defaults to |
fill | Value with which to fill missing cells. Default uses |
data.table
The parameter of `name` and `value` should always be provided and should be explicit called (with the parameter names attached).
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) ) %>% longer_dt(time) -> longer_stocks longer_stocks#> time name value #> <Date> <fctr> <num> #> 1: 2009-01-01 X 1.07283825 #> 2: 2009-01-02 X 2.03936926 #> 3: 2009-01-03 X 0.44945378 #> 4: 2009-01-04 X 1.39181405 #> 5: 2009-01-05 X 0.42656655 #> 6: 2009-01-06 X 0.10758399 #> 7: 2009-01-07 X 0.02229473 #> 8: 2009-01-08 X 0.60361101 #> 9: 2009-01-09 X -0.26265057 #> 10: 2009-01-10 X -0.52826408 #> 11: 2009-01-01 Y 0.38429884 #> 12: 2009-01-02 Y -2.29239934 #> 13: 2009-01-03 Y 1.69236933 #> 14: 2009-01-04 Y 0.16343926 #> 15: 2009-01-05 Y -2.61023402 #> 16: 2009-01-06 Y -1.88982412 #> 17: 2009-01-07 Y 0.90868319 #> 18: 2009-01-08 Y -1.71040500 #> 19: 2009-01-09 Y -0.57379044 #> 20: 2009-01-10 Y 1.78992325 #> 21: 2009-01-01 Z 0.26921776 #> 22: 2009-01-02 Z -0.65070535 #> 23: 2009-01-03 Z -3.30924068 #> 24: 2009-01-04 Z 7.50602248 #> 25: 2009-01-05 Z 3.06576080 #> 26: 2009-01-06 Z 3.91982678 #> 27: 2009-01-07 Z 5.28712397 #> 28: 2009-01-08 Z -4.47884331 #> 29: 2009-01-09 Z 2.05839927 #> 30: 2009-01-10 Z -6.03639934 #> time name valuelonger_stocks %>% wider_dt("time", name = "name", value = "value")#> Key: <time> #> time X Y Z #> <Date> <num> <num> <num> #> 1: 2009-01-01 1.07283825 0.3842988 0.2692178 #> 2: 2009-01-02 2.03936926 -2.2923993 -0.6507053 #> 3: 2009-01-03 0.44945378 1.6923693 -3.3092407 #> 4: 2009-01-04 1.39181405 0.1634393 7.5060225 #> 5: 2009-01-05 0.42656655 -2.6102340 3.0657608 #> 6: 2009-01-06 0.10758399 -1.8898241 3.9198268 #> 7: 2009-01-07 0.02229473 0.9086832 5.2871240 #> 8: 2009-01-08 0.60361101 -1.7104050 -4.4788433 #> 9: 2009-01-09 -0.26265057 -0.5737904 2.0583993 #> 10: 2009-01-10 -0.52826408 1.7899233 -6.0363993#> Key: <time> #> time X Y Z #> <Date> <num> <num> <num> #> 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## using "fun" parameter for aggregation DT <- data.table(v1 = rep(1:2, each = 6), v2 = rep(rep(1:3, 2), each = 2), v3 = rep(1:2, 6), v4 = rnorm(6)) ## for each combination of (v1, v2), add up all values of v4 DT %>% wider_dt(v1,v2, value = "v4", name = ".", fun = sum)#> Key: <v1, v2> #> v1 v2 . #> <int> <int> <num> #> 1: 1 1 1.9618889 #> 2: 1 2 -1.0159090 #> 3: 1 3 0.4945234 #> 4: 2 1 1.9618889 #> 5: 2 2 -1.0159090 #> 6: 2 3 0.4945234