I have a program that uses reshape2's melt function to melt a 5-dimensional array with named and labelled dimensions to a long-form data frame, which by definition has only two dimensions. Each dimension of the input array corresponds to a column in the output data frame, and there is one more column that holds the values that were stored in the 5D array.
I understand reshape2 is deprecated and will soon break. So I am changing to tidyr. However tidyr's pivot_longer function that replaces melt only accepts 2D data frames as inputs.
Is there a non-deprecated function, in tidyr or elsewhere, that will melt an array with 3 or more named and labelled dimensions to a long form data frame?
I could write my own function to do it easily enough. But I'd rather use an existing function if there is one.
Thank you
Here's an example of 2x3x4 array:
df <- expand.grid(w = 1:2,
x = 1:3,
y = 1:4)
df$z <- runif(nrow(df))
tmp <- tapply(df$z, list(df$w, df$x, df$y), sum)
tmp
, , 1
1 2 3
1 0.40276418 0.13111652 0.4473557
2 0.08945365 0.03139184 0.1556355
, , 2
1 2 3
1 0.1413763 0.02106974 0.1103559
2 0.7302435 0.46302772 0.7924580
, , 3
1 2 3
1 0.2793435 0.4244807 0.7955351
2 0.9828739 0.7740189 0.6436733
, , 4
1 2 3
1 0.9852345 0.20508490 0.8744829
2 0.2812744 0.06272449 0.0936831