Transform a data frame from long format to wide by increasing the number of columns and decreasing the number of rows.

wider_dt(.data, ..., name, value = NULL, fun = NULL, fill = NA)

Arguments

.data

A data.frame

...

Optional. The unchanged group in the transformation. Could use integer vector, could receive what select_dt receives.

name

Chracter.One column name of class to spread

value

Chracter.One column name of value to spread. If NULL, use all other variables.

fun

Should the data be aggregated before casting? Defaults to NULL, which uses length for aggregation. If a function is provided, with aggregated by this function.

fill

Value with which to fill missing cells. Default uses NA.

Value

data.table

Details

The parameter of `name` and `value` should always be provided and should be explicit called (with the parameter names attached).

Examples

 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  0.335008866
#>  2: 2009-01-02      X  0.169063399
#>  3: 2009-01-03      X  1.387121164
#>  4: 2009-01-04      X  1.378852616
#>  5: 2009-01-05      X  0.393258277
#>  6: 2009-01-06      X  0.096138697
#>  7: 2009-01-07      X  1.313206156
#>  8: 2009-01-08      X  0.013035822
#>  9: 2009-01-09      X -1.281642993
#> 10: 2009-01-10      X -0.004285721
#> 11: 2009-01-01      Y  2.054135393
#> 12: 2009-01-02      Y -0.166088761
#> 13: 2009-01-03      Y  0.841223197
#> 14: 2009-01-04      Y -1.681912731
#> 15: 2009-01-05      Y  0.276743427
#> 16: 2009-01-06      Y -1.477213273
#> 17: 2009-01-07      Y  5.715596062
#> 18: 2009-01-08      Y  0.258267190
#> 19: 2009-01-09      Y  1.349114073
#> 20: 2009-01-10      Y  0.062882772
#> 21: 2009-01-01      Z  2.921755755
#> 22: 2009-01-02      Z -4.169214230
#> 23: 2009-01-03      Z  5.534706702
#> 24: 2009-01-04      Z  6.052646574
#> 25: 2009-01-05      Z  5.095414544
#> 26: 2009-01-06      Z  4.402146914
#> 27: 2009-01-07      Z -5.316178929
#> 28: 2009-01-08      Z -2.130851174
#> 29: 2009-01-09      Z -3.674280881
#> 30: 2009-01-10      Z  0.775161779
#>           time   name        value

 longer_stocks %>%
   wider_dt("time",
            name = "name",
            value = "value")
#> Key: <time>
#>           time            X           Y          Z
#>         <Date>        <num>       <num>      <num>
#>  1: 2009-01-01  0.335008866  2.05413539  2.9217558
#>  2: 2009-01-02  0.169063399 -0.16608876 -4.1692142
#>  3: 2009-01-03  1.387121164  0.84122320  5.5347067
#>  4: 2009-01-04  1.378852616 -1.68191273  6.0526466
#>  5: 2009-01-05  0.393258277  0.27674343  5.0954145
#>  6: 2009-01-06  0.096138697 -1.47721327  4.4021469
#>  7: 2009-01-07  1.313206156  5.71559606 -5.3161789
#>  8: 2009-01-08  0.013035822  0.25826719 -2.1308512
#>  9: 2009-01-09 -1.281642993  1.34911407 -3.6742809
#> 10: 2009-01-10 -0.004285721  0.06288277  0.7751618

 longer_stocks %>%
   mutate_dt(one = 1) %>%
   wider_dt("time",
            name = "name",
            value = "one")
#> 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  0.1575282
#> 2:     1     2  0.6787566
#> 3:     1     3 -0.5871757
#> 4:     2     1  0.1575282
#> 5:     2     2  0.6787566
#> 6:     2     3 -0.5871757