Description Usage Arguments Value Examples
View source: R/reshape_longer.R
reshape_longer()
reshapes one or more columns from
wide into long format.
1 2 | reshape_longer(x, columns = colnames(x), names_to = "key",
values_to = "value", numeric_timvar = FALSE, id = "id")
|
x |
A data frame. |
columns |
Names of variables (as character vector), or column index of variables, that should be reshaped. If multiple column groups should be reshaped, use a list of vectors (see 'Examples'). |
names_to |
todo |
values_to |
todo |
numeric_timvar |
todo |
id |
todo |
A reshaped data frame.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | d <- data.frame(
x = 1:4,
y1 = rnorm(4),
y2 = rnorm(4),
a = c(1, 1, 0, 0),
b = c(0, 1, 1, 1)
)
# simple reshape of one time-varying variable
reshape_longer(
d,
columns = c("y1", "y2"),
names_to = "time",
values_to = "new_y"
)
d <- data.frame(
x = 1:4,
y1 = rnorm(4),
y2 = rnorm(4),
z1 = rep(3, 4),
z2 = rep(-2, 4),
a = c(1, 1, 0, 0),
b = c(0, 1, 1, 1)
)
# reshape multiple time-varying variables
reshape_longer(
d,
columns = list(c("y1", "y2"), c("z1", "z2")),
names_to = "time",
values_to = c("new_y", "new_z"),
numeric_timvar = TRUE
)
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.