if (FALSE) { # requireNamespace("psych") && requireNamespace("tidyr")
wide_data <- setNames(
data.frame(replicate(2, rnorm(8))),
c("Time1", "Time2")
)
wide_data$ID <- 1:8
wide_data
# Default behaviour (equivalent to tidyr::pivot_longer(wide_data, cols = 1:3))
# probably doesn't make much sense to mix "time" and "id"
data_to_long(wide_data)
# Customizing the names
data_to_long(
wide_data,
select = c("Time1", "Time2"),
names_to = "Timepoint",
values_to = "Score"
)
# Reshape multiple columns into long format.
mydat <- data.frame(
age = c(20, 30, 40),
sex = c("Female", "Male", "Male"),
score_t1 = c(30, 35, 32),
score_t2 = c(33, 34, 37),
score_t3 = c(36, 35, 38),
speed_t1 = c(2, 3, 1),
speed_t2 = c(3, 4, 5),
speed_t3 = c(1, 8, 6)
)
# The column names are split into two columns: "type" and "time". The
# pattern for splitting column names is provided in `names_pattern`. Values
# of all "score_*" and "speed_*" columns are gathered into a single column
# named "count".
data_to_long(
mydat,
select = 3:8,
names_to = c("type", "time"),
names_pattern = "(score|speed)_t(\\d+)",
values_to = "count"
)
# Full example
# ------------------
data <- psych::bfi # Wide format with one row per participant's personality test
# Pivot long format
very_long_data <- data_to_long(data,
select = regex("\\d"), # Select all columns that contain a digit
names_to = "Item",
values_to = "Score",
rows_to = "Participant"
)
head(very_long_data)
even_longer_data <- data_to_long(
tidyr::who,
select = new_sp_m014:newrel_f65,
names_to = c("diagnosis", "gender", "age"),
names_pattern = "new_?(.*)_(.)(.*)",
values_to = "count"
)
head(even_longer_data)
}
Run the code above in your browser using DataLab