longer | R Documentation |
Fast table pivoting from long to wide and from wide to long.
These functions are supported by dcast.data.table
and melt.data.table
from data.table.
longer(.data, ..., name = "name", value = "value", na.rm = FALSE)
wider(.data, ..., name, value = NULL, fun = NULL, fill = NA)
.data |
A data.table |
... |
Columns for unchanged group. Flexible, see examples. |
name |
Name for the measured variable names column. |
value |
Name for the data values column(s). |
na.rm |
If |
fun |
Should the data be aggregated before casting?
Defaults to |
fill |
Value with which to fill missing cells. Default uses |
A data.table
longer_dt
,wider_dt
stocks <- data.table(
time = as.Date('2009-01-01') + 0:9,
X = rnorm(10, 0, 1),
Y = rnorm(10, 0, 2),
Z = rnorm(10, 0, 4)
)
stocks %>% longer(time)
stocks %>% longer(-(2:4)) # same
stocks %>% longer(-"X|Y|Z") # same
long_stocks = longer(stocks,"ti") # same as above except for assignment
long_stocks %>% wider(time,name = "name",value = "value")
# the unchanged group could be missed if all the rest will be used
long_stocks %>% wider(name = "name",value = "value")
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.