描述
通过增加列数和减少行数,将data_frame从长格式转换为宽格式。
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
longer_stocks %>%
wider_dt("time",
name = "name",
value = "value")
longer_stocks %>%
mutate_dt(one = 1) %>%
wider_dt("time",
name = "name",
value = "one")
## 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)