--- title: "Reshape R dataframes from wide to long with melt" description: | Learn and visualize how melt reshapes dataframes from long to wide author: - name: Hause Lin url: {} date: 06-10-2020 draft: false preview: ./main_wide2long.png categories: - melt - reshape - data.table output: radix::radix_article: toc: true self_contained: false editor_options: chunk_output_type: console --- ```{r setup, include=FALSE} knitr::opts_chunk$set(echo = TRUE, cache = FALSE, comment = NA, message = FALSE, warning = FALSE) ``` This tutorial is also on [Medium, Towards Data Science](https://towardsdatascience.com/reshape-r-dataframes-wide-to-long-with-melt-tutorial-and-visualization-ddf130cd9299). Get source code for this RMarkdown script [here](https://raw.githubusercontent.com/hauselin/rtutorialsite/master/_posts/2020-06-27-reshape-r-dataframes-from-wide-to-long-with-melt/reshape-r-dataframes-from-wide-to-long-with-melt.Rmd). ## Consider being a patron and supporting my work? [Donate and become a patron](https://donorbox.org/support-my-teaching): If you find value in what I do and have learned something from my site, please consider becoming a patron. It takes me many hours to research, learn, and put together tutorials. Your support really matters. How do you reshape a dataframe from wide to long form in R? How does the `melt()` function reshape dataframes in R? This tutorial will walk you through reshaping dataframes using the `melt` function. ![Summary of how melt() works](./main_wide2long.png) Common terms for this transformation are melt, pivot-long, unpivot, gather, stack, and reshape. Many functions have been written to convert data from wide to long form, but I believe `melt()` from the `data.table` library is the best. See `melt()` documentation [here](https://cran.r-project.org/web/packages/data.table/vignettes/datatable-reshape.html). * Python's [pandas](https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.melt.html) library also has the equivalent `melt` function/method that works the same way (see my pandas melt tutorial [here](https://towardsdatascience.com/reshape-pandas-dataframe-with-melt-in-python-tutorial-and-visualization-29ec1450bb02)) * other functions like `gather` and `pivot_longer` are often just wrapper functions for `melt()` or `reshape()`—these other functions simplify `melt` and often can't deal with more complex transformations * `melt` is more powerful but isn't any more complicated than the other functions... * `melt` alone is often enough for all your wide-to-long transformations; don't need to learn `gather`, `pivot_longer`, and `melt`; `melt` alone is enough! * `data.table` package's implementation of `melt`, which is extremely powerful—much more efficient and powerful than the reshape library's `melt` function. According to the [documentation](https://cran.r-project.org/web/packages/data.table/vignettes/datatable-reshape.html): > The melt and dcast functions for data.tables are for reshaping wide-to-long and long-to-wide, respectively; the implementations are specifically designed with large in-memory data (e.g. 10Gb) in mind. Reminder: We're using `melt` from the `data.table` library, not `reshape` library! Compare the documentation of the `melt` functions from the two libraries to the differences: `?data.table::melt` and `?reshape::melt` ```{r} # load data.table library so we use its melt implementation library(data.table) ``` ## Wide data It’s easiest to understand what a **wide** dataframe is or looks like if we look at one and compare it with a long dataframe.