Converts a vintages dataset from wide format to long format, optionally
adding id if the input is a list of data frames. The long format contains
one row per combination of time and names_to (e.g., pub_date or
release), with values stored in a single value column.
Arguments
- df
A data frame, tibble, or list of data frames containing vintages data in wide format.
- names_to
The name of the column to create from the wide-format column names. Must be either
"pub_date"(default) or"release".- keep_na
Logical. If
TRUE, retains rows withNAvalues in thevaluecolumn. Default isFALSE.
Value
A long-format vintages object: a tibble carrying the tbl_pubdate
or tbl_release class and their shared parent tbl_vintage. If the input
is a list of wide-format data frames, the output is a single combined
long-format object.
Long-format input is returned with the vintages class attached, which is
also how to recover the class after an operation that dropped it (see the
"Operations that drop the class" section of validate_vintages()). Input
that is already a long-format vintages object warns, because the call is
then a no-op.
Examples
# Example wide-format data
long_data <- dplyr::filter(reviser::gdp, id == "US")
# Convert to wide format
wide_data <- vintages_wide(long_data)
# Example list of wide-format data frames
wide_list <- list(
A = wide_data$US,
B = wide_data$US
)
# Convert list to long format
long_data <- vintages_long(wide_list, names_to = "pub_date")
