# \donttest{
wide_data <- data.frame(replicate(5, rnorm(10)))
# Default behaviour (equivalent to tidyr::pivot_longer(wide_data, cols = 1:5))
data_to_long(wide_data)
# Customizing the names
data_to_long(wide_data,
select = c(1, 2),
names_to = "Column",
values_to = "Numbers",
rows_to = "Row"
)
# Full example
# ------------------
if (require("psych")) {
data <- psych::bfi # Wide format with one row per participant's personality test
# Pivot long format
data_to_long(data,
select = regex("\\d"), # Select all columns that contain a digit
names_to = "Item",
values_to = "Score",
rows_to = "Participant"
)
if (require("tidyr")) {
reshape_longer(
tidyr::who,
select = new_sp_m014:newrel_f65,
names_to = c("diagnosis", "gender", "age"),
names_pattern = "new_?(.*)_(.)(.*)",
values_to = "count"
)
}
}
# }
Run the code above in your browser using DataLab