In the realm of data manipulation and analysis, converting a table from wide to long format is a common and powerful operation. This transformation allows you to reshape your data from a format where variables are columns to a format where each observation is a row, making it easier to analyze and visualize your data. In this article, we will delve into the process of converting a table to long format, using the popular programming language R and its extensive suite of data manipulation libraries.
Understanding Wide and Long Format
Before we dive into the conversion process, let's ensure we understand the difference between wide and long format. In wide format, each row represents a unique observation, and variables are columns. In long format, each row represents a unique combination of an observation and a variable, making it ideal for analysis and visualization.
Preparing Your Data
Before converting your table to long format, ensure your data is in a tidy format. This means each variable should have its own column, and each observation should have its own row. If your data is messy, consider using R's `tidyr` package to clean it up using functions like `gather()` and `spread()`.

Gathering Data
The `gather()` function is the primary tool for converting a table from wide to long format. It takes a data frame and a list of columns, and combines them into key-value pairs. Here's a simple example:
```R library(tidyr) # Wide format data frame wide_data <- data.frame( id = 1:3, var1 = c(10, 20, 30), var2 = c(40, 50, 60) ) # Gather data long_data <- gather(wide_data, key = "variable", value = "value", -id) ```
Spreading Data
While `gather()` is used to convert from wide to long format, `spread()` does the opposite. It takes a data frame and a key-value pair, and spreads the values into new columns based on the key. This can be useful if you need to clean up your data before converting it to long format.
Converting a Table to Long Format with `gather()`
Now that we've prepared our data, let's look at how to use `gather()` to convert a table to long format. The basic syntax is:

```R long_data <- gather(data, key = "variable", value = "value", -column) ```
Here, `data` is your data frame, `key` is the name you want to give to the new column that will hold the variable names, `value` is the name you want to give to the new column that will hold the variable values, and `-column` tells `gather()` to exclude certain columns from being gathered. In the following example, we'll gather all columns except `id`:
```R # Wide format data frame wide_data <- data.frame( id = 1:3, var1 = c(10, 20, 30), var2 = c(40, 50, 60), var3 = c(70, 80, 90) ) # Gather data long_data <- gather(wide_data, key = "variable", value = "value", -id) ```
This will create a new data frame `long_data` where each row represents a unique combination of an observation (`id`) and a variable (`variable` and `value`).
Handling Multiple Variables
What if you have multiple variables that you want to gather? You can specify multiple columns to gather like this:
```R long_data <- gather(wide_data, key = "variable", value = "value", var1, var2, var3) ```
This will gather `var1`, `var2`, and `var3` into the `variable` and `value` columns.
Using `pivot_longer()`
In the latest version of `tidyr`, the `gather()` function has been superseded by `pivot_longer()`. This function is more flexible and can handle more complex data structures. Here's how you can use it to convert a table to long format:
```R long_data <- pivot_longer(wide_data, cols = c(var1, var2, var3), names_to = "variable", values_to = "value") ```
Here, `cols` specifies the columns to gather, `names_to` gives the name of the new column that will hold the variable names, and `values_to` gives the name of the new column that will hold the variable values.
Conclusion
Converting a table from wide to long format is a powerful tool for data analysis and visualization. Whether you're using `gather()` or `pivot_longer()`, these functions provide a straightforward way to reshape your data and unlock its full potential. With practice, you'll find that converting tables to long format becomes second nature, and you'll be well on your way to mastering the art of data manipulation in R.
More Details
02.02.2010 ... I have some trouble to convert my data.frame from a wide table to a long table. ... Converting a dataframe from "wide" format to "long" format in ...

You want to do convert data from a wide format to a long format. Many ... table(header=TRUE, text=' subject sex control cond1 cond2 1 M 7.9 12.3 10.7 ...

22.07.2025 ... reshape(): Built-in R function for converting data between wide and long formats. · direction = "long": Indicates conversion from wide to long.

Pivot data from wide to long. Source: R/pivot-long.R. pivot_longer.Rd ... < tidy-select > Columns to pivot into longer format. ... Additional arguments ...

Use pivot_wider() to reshape a table from long to wide format. Use pivot_longer() to reshape a table from wide to long format. To figure out which data format ...

15.07.2014 ... Basically, reshape2 is based around two key functions: melt and cast: melt takes wide-format data and melts it into long-format data. cast takes ...

13.07.2019 ... ... convert a 10-thousand-column survey data that I was working with from a wide format to a long …

This tutorial explains how to convert data from wide to long format with R programming. Note : Before running the code below, Install reshape2 package if not ...

27.06.2021 ... How to convert long to wide table format? r/excel - How to convert long to wide table format?

melt() method on a DataFrame converts the data table from wide format to long format. The column headers become the variable names in a newly created column.

29.04.2023 ... Wide-to-long format conversion is often an important data manipulation technique in data analysis. In R, we can use many packages and their ...

07.04.2026 ... Sub NarrowToWide() Dim wsSource As Worksheet Dim wsTarget As Worksheet Dim lastRow As Long Dim dict As Object Dim r As Long Dim timeKey As ...

19.11.2022 ... As a rule of thumb, if you're visualizing multiple variables in a plot using statistical software such as R you typically must convert your data ...

15.12.2022 ... But you need to think it in a long run, every time you clean up those fields manually can add up a huge ton of time. We as data analysts have ...

scatter expect to operate on column-oriented data of the type you might store in a DataFrame (in either "long" or "wide" format, see below). These functions ...

04.05.2026 ... Allowing j to return a list enables converting and returning data.table very efficiently. Tip: As long as j-expression returns a list , each ...

This is called a wide format since the years of data are wide. We may want the data to be long, where each year of data is in a separate observation. The ...

Luckily, converting from one to the other is generally not too difficult in ... For example, it's often easier to enter and manipulate data in the wide format, ...

21.11.2023 ... Want to learn how to use ChatGPT to write Python code to stack a data file (i.e., convert from wide to long format)?

19.01.2023 ... A dataset can be written in two different formats: wide and long. A wide format contains values that do not repeat in the first column while A ...
