База R Решение:
# Transpose dataframe matrix: tpd => as.data.frame
tpd <- as.data.frame(t(df))
# Restructure the dataframe into the desired format: df_td => data.frame
df_td <-
data.frame(
day = gsub("_.*", "", rep(row.names(tpd), ncol(tpd))),
group = gsub(".*_", "", rep(row.names(tpd), ncol(tpd))),
quantitative = unlist(tpd),
row.names = NULL
)
Данные
# Create re-usable data: df => data.frame
df <-
structure(
list(
monday_A = c(1L, 6L),
monday_B = c(2L, 7L),
tuesday_A = c(4L,
8L),
tuesday_B = c(100L, 5L)
),
row.names = c(NA,-2L),
class = "data.frame"
)