I have a series of columns in my data frame (299 of them) which have the same information type repeated every 3 columns. I need to stack them so each set of 3 retain their referential integrity with each other, and the preceding 22 columns. Normally I'd use melt() to go from wide to long form, but that would involve one or two categories with common names. Each of these columns has a unique name (e.g. "x1", "y1", "z1","x2","y2","z2").
For the sake of example, lets take example data mtcars. These data contain the 11 columns, each with unique names: mpg, cyl, disp, hp, drat, wt, qsec, vs, am, gear, carb. Lets say I want to keep columns 1:5 as identifiers, then stack the data columns 6:8 on top of 9:11. In this simple example I could use stack or melt and say which columns to use, but when we extrapolate out to hundreds of columns this gets to be a pretty horrid task very quickly... thoughts?
x
get stacked together? Or is it purely positional? Using themtcars
example, cols1:5
as you say are identifiers, then for6:ncol(mtcars)
it's is sets of 3? – Gregor Thomasdata.table::melt
andtidyr::pivot_longer
are very powerful as to which columns get stacked and could handle a naming pattern.data.table::melt
will work well with column numbers too. – Gregor Thomasdata.table::melt
.reshape2::melt
doesn't have that feature, butdata.table::melt
lets you pass alist
tomeasure.vars
. For every 3 columns between 2 and 1000 you would usemeasure.vars = split(2:1000, (seq_along(2:1000) - 1) %/% 3)
. But with a names patterntidyr::pivot_longer
is also easy. – Gregor Thomas