We can use melt
from data.table
which can take multiple measure
columns with the pattern
argument. We convert the 'data.frame' to 'data.table' (setDT(data)
), then melt
to 'long' format.
library(data.table)
DT <- melt(setDT(data), measure=patterns('Mean$', 'Num$'),
variable.name='Type', value.name=c('Mean', 'Num'))
DT[, Type:=c('Cab', 'Part', 'Dinn')[Type]]