df.long | R Documentation |
The function df.long
converts a data frame from the 'wide' data format
(with repeated measurements in separate columns of the same row) to the 'long'
data format (with repeated measurements in separate rows), while the function
df.wide
converts from the 'long' data format to the 'wide' data format
.
df.long(data, ..., var = NULL, var.name = "value",
time = c("num", "chr", "fac", "ord"), time.name = "time", idvar = "idvar",
sort = TRUE, decreasing = FALSE, na.rm = FALSE, check = TRUE)
df.wide(data, ..., var, var.name = var, time = "time", idvar = "idvar",
sep = "", check = TRUE)
data |
a data frame in 'wide' or 'long' format. |
... |
an expression indicating the time-invariant variable names
in |
var |
a character vector (one set of variable names) or a list of
character vectors (multiple sets of variables names) in
the wide data format indicating the sets of time-varying
variables in the wide format that correspond to single
variables in the long format when using the |
var.name |
a character vector specifying the variable names in the long
format that correspond to the sets of time-varying variables
in the wide data format when using the |
time |
a character string indicating the data type of the newly
created variable in the long format when using the |
time.name |
a character string indicating the name of the newly created
variable in the long format when using the |
idvar |
a character string indicating the name of the identification
variable in the wide data format that is used to sort the
data after converting a data frame from wide to long format
when using the |
sort |
logical: if |
decreasing |
logical: if TRUE, the sort is decreasing when specifying
|
na.rm |
logical: if TRUE, rows with |
check |
logical: if TRUE (default), argument specification is checked. |
sep |
a character string indicating a separating character in the
variable names after converting data from the long format
to the wide format when using the |
Data frame that is converted to the 'long' or 'wide' format.
The function df.long
uses the function melt
and the function
df.long
uses the function dcast
provided in the R package
data.table by Tyson Barrett et al., (2025).
Takuya Yanagida
Barrett, T., Dowle, M., Srinivasan, A., Gorecki, J., Chirico, M., Hocking, T., & Schwendinger, B. (2025). data.table: Extension of 'data.frame'. R package version 1.17.8. https://CRAN.R-project.org/package=data.table
df.check
, df.duplicated
, df.unique
,
df.head
, df.tail
,
df.merge
, df.move
,
df.rbind
, df.rename
, df.sort
,
df.subset
dat.w <- data.frame(id = c(23, 55, 71),
gend = c("male", "female", "male"), age = c(22, 19, 26),
adep = c(3, 6, NA), bdep = c(5, 5, 6), cdep = c(4, NA, 5),
aanx = c(5, 3, 6), banx = c(NA, 7, 2), canx = c(6, NA, 8))
#----------------------------------------------------------------------------
# Convert from 'wide' data format to the 'long' data format
# Example 1: One set of time-varying variables combined into "dep"
df.long(dat.w, var = c("adep", "bdep", "cdep"), var.name = "dep", idvar = "id")
# Example 2: Select time-invariant variables 'gend' and 'age'
df.long(dat.w, gend, age, var = c("adep", "bdep", "cdep"), var.name = "dep",
idvar = "id")
# Example 3: Newly created variable "type" as character vector
df.long(dat.w, age, var = c("adep", "bdep", "cdep"), var.name = "dep",
idvar = "id", time = "chr", time.name = "type")
# Example 4: User-defined variable "type"
df.long(dat.w, age, var = c("adep", "bdep", "cdep"), var.name = "dep",
idvar = "id", time = c("pre", "post", "follow-up"), time.name = "type")
# Example 5: Two sets of time-varying variables combined into "dep" and "anx"
df.long(dat.w, age,
var = list(c("adep", "bdep", "cdep"), c("aanx", "banx", "canx")),
var.name = c("dep", "anx"), idvar = "id")
# Alternative specification using named lists for the argument 'var'
df.long(dat.w, age,
var = list(dep = c("adep", "bdep", "cdep"), anx = c("aanx", "banx", "canx")),
idvar = "id")
# Example 6: Remove rows with only NA values
df.long(dat.w, age, var = list(c("adep", "bdep", "cdep"), c("aanx", "banx", "canx")),
idvar = "id", sort = FALSE, na.rm = TRUE)
# Example 7: Convert all variables except "age" and "gend"
df.long(dat.w, age, gend, idvar = "id")
#----------------------------------------------------------------------------
# Convert from 'long' data format to the 'wide' data format
dat.l <- df.long(dat.w,
var = list(c("adep", "bdep", "cdep"), c("aanx", "banx", "canx")),
var.name = c("dep", "anx"), idvar = "id")
# Example 8: Time-varying variables "dep" and "anx" expanded into multiple variables
df.wide(dat.l, var = c("dep", "anx"), idvar = "id", time = "time")
# Example 9: Select time-invariant variables 'age'
df.wide(dat.l, age, var = c("dep", "anx"), idvar = "id", time = "time")
# Example 10: Variable name prefix of the expanded variables "depre" and "anxie"
# with separating character "."
df.wide(dat.l, var = c("dep", "anx"), var.name = c("depre", "anxie"),
idvar = "id", time = "time", sep = ".")
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.