What is the equivalent of reshape2's melt in collapse?

113 Views Asked by At

I melt data frames using the (now deprecated) R package reshape2 for plotting using ggplot2, but just heard of collapse, a high performance package for data transformation. Unfortunately, I could not determine the equivalent of melt by looking through the cheat sheet provided at https://github.com/SebKrantz/collapse. I would be very appreciative if someone could share the the syntax for melting a data frame using collapse.

1

There are 1 best solutions below

0
Sebastian On

{collapse}'s reshape command is called pivot(), and it does longer, wider and recast pivoting all in one (controlled by the how argument, default how = "longer", so by default it very much works like melt). Many examples are provided in the online documentation, and the function is also introduced with some discussion in the Blog Post announcing collapse 2.0. To just provide the first 3 examples from the documentation here:

library(collapse)
#> collapse 2.0.3, see ?`collapse-package` or ?`collapse-documentation`

pivot(mtcars) |> head()
#>   variable value
#> 1      mpg  21.0
#> 2      mpg  21.0
#> 3      mpg  22.8
#> 4      mpg  21.4
#> 5      mpg  18.7
#> 6      mpg  18.1
pivot(iris, "Species") |> head()
#>   Species     variable value
#> 1  setosa Sepal.Length   5.1
#> 2  setosa Sepal.Length   4.9
#> 3  setosa Sepal.Length   4.7
#> 4  setosa Sepal.Length   4.6
#> 5  setosa Sepal.Length   5.0
#> 6  setosa Sepal.Length   5.4
pivot(iris, values = 1:4) |> head() # Same thing
#>   Species     variable value
#> 1  setosa Sepal.Length   5.1
#> 2  setosa Sepal.Length   4.9
#> 3  setosa Sepal.Length   4.7
#> 4  setosa Sepal.Length   4.6
#> 5  setosa Sepal.Length   5.0
#> 6  setosa Sepal.Length   5.4

Created on 2023-10-24 with reprex v2.0.2