The pivot_longer() function from the tidyr package reshapes a data frame from a wide format to a long format, making the data tidy and more compact by transforming multiple columns into key-value pairs. As an updated and more versatile alternative to gather(), pivot_longer() is simpler to use and covers a broader range of use cases. While gather() is still available, it is no longer actively developed, so it is recommended to use pivot_longer() in new code.
In this article, I will explore the pivot_longer() function, detailing its syntax, parameters, and practical use cases. Through clear examples, I will demonstrate how to transform data frames into long formats for better usability.
Key Points-
- pivot_longer function to reshape wide-format data into long format.
- Customize key and value column names with
names_toandvalues_to. - Remove rows with missing values using
values_drop_na = TRUE. - Handle duplicate or invalid column names with
names_repair. - Use
names_prefix,names_sep, andnames_patternfor advanced name handling. - Returns a tidy tibble in a long format.
- Simplifies data preparation for visualization and analysis.
- It replaces
gather()with broader functionality and active development.
<a id="h-r-pivot-longer-function"></a>
R pivot_longer() Function
The pivot_longer() function in the tidyr package is essential for transforming data into a longer format. It is particularly useful for converting columns into rows, resulting in a tidy dataset with more rows and fewer columns. This highly customizable option, allows you to select which columns to pivot, rename new columns, and more.
<a id="h-syntax-of-pivot-longer-function"></a>
Syntax of pivot_longer Function
The general syntax of the pivot_longer() function.
# Syntax of the pivot_longer() function
pivot_longer(data, cols, names_to = "name", values_to = "value", ...)
<a id="h-parameters"></a>
Parameters
data:The input data frame or tibble.cols:The columns to pivot to the longer format.names_to:Name of the new column for key names. Defaults to"name".values_to:Name of the new column for values. Defaults to"value".values_drop_na:Drops rows where the pivoted value isNAif set toTRUE...:Additional arguments for controlling names and values
<a id="h-return-value"></a>
Return Value
It returns a data frame or tibble in a long format, combining selected columns into key-value pairs.
<a id="h-transform-to-long-format-with-r-pivot-longer"></a>
Transform to Long Format with R pivot_longer()
You can use pivot_longer() to convert data from wide format to long format. Simply create a data frame and pass the columns you want to pivot, along with the data frame, to the function. This will transform the specified column names and their values into rows in the resulting data frame.
# Transforming data frame from wide to long format
# Load tidyr package
library(tidyr)
# Create data frame
df <- data.frame(
Student = c("Geetha", "Ram", "Sai"),
Math = c(85, 90, 78),
Science = c(80, 85, 88),
History = c(90, 92, 85)
)
print("Original Data Frame:")
print(df)
long_df <- pivot_longer(
data = df,
cols = c(Math, Science, History)
)
print("Data Frame in Long Format:")
print(long_df)
Yields below output.
<a id="h-customize-column-names-with-r-pivot-longer"></a>
Customize Column Names with R pivot_longer()
You can customize the column names in the longer data frame using the pivot_longer() function. To do this, specify the names_to and values_to arguments with the desired column names. Pass them along with the data frame and the columns you want to pivot. The function will create a longer data frame with the specified column names.
# Customize Column Names with pivot_longer()
long_df <- pivot_longer(
data = df,
cols = c(Math, Science, History),
names_to = "Subjects",
values_to = "Marks"
)
print("Customized Column Names:")
print(long_df)
Yields below output.
<a id="h-handling-missing-values-in-pivot-longer"></a>
Handling Missing Values in pivot_longer()
You can remove rows with missing values in the longer data frame using the pivot_longer() function. To achieve this, set the values_drop_na parameter to TRUE and pass it to the function. This will exclude any NA values from the resulting longer data frame.
# Handling missing Values in pivot_longer()
# Load tidyr package
library(tidyr)
df <- data.frame(
Student = c("Geetha", "Ram", "Sai"),
Math = c(85, NA, 78),
Science = c(80, 85, 88),
History = c(NA, 92, 85)
)
# Pivot and drop NA values
long_df <- pivot_longer(
data = df,
cols = c(Math, Science, History),
names_to = "Subject",
values_to = "Score",
values_drop_na = TRUE
)
print("After Dropping NA Values:")
print(long_df)
Yields below output.
# Output:
[1] "After Dropping NA Values:"
> print(long_df)
# A tibble: 7 × 3
Student Subject Score
1 Geetha Math 85
2 Geetha Science 80
3 Ram Science 85
4 Ram History 92
5 Sai Math 78
6 Sai Science 88
7 Sai History 85
<a id="h-handling-duplicate-or-invalid-names-with-names-repair"></a>
Handling Duplicate or Invalid Names with names_repair
To manage duplicate column names in the longer format data frame, you can use the pivot_longer() function in R. Simply set the names_repair parameter to "unique" and pass it to the function. This will return the resulting data frame having unique column names along with their corresponding values.
# Handling duplicate column names
# Load tidyr package
library(tidyr)
# Create a data frame
df <- data.frame(
"Math_2023" = c(85, 90, 78),
"Math_2023" = c(88, 92, 81),
check.names = FALSE
)
# Repairing duplicate column names
long_df <- pivot_longer(
data = df,
cols = everything(),
names_to = "Subject_Year",
names_repair = "unique",
values_to = "Score"
)
print("After Repairing Duplicate Names:")
print(long_df)
Yields below output.
# Output:
[1] "After Repairing Duplicate Names:"
> print(long_df)
# A tibble: 6 × 2
Subject_Year Score
1 Math_2023 85
2 Math_2023 88
3 Math_2023 90
4 Math_2023 92
5 Math_2023 78
6 Math_2023 81
<a id="h-frequently-asked-questions-of-pivot-longer"></a>
Frequently Asked Questions of pivot_longer()
The pivot_longer() function reshapes data from wide format to long format, combining multiple columns into key-value pairs.
You can use names_prefix alongside names_sep or names_pattern to further customize the pivoting process.
If not specified, missing values are retained in the long-format data.
You can use the values_drop_na parameter to remove rows with missing values in the resulting long-format data.
pivot_longer() is newer, more versatile, and actively maintained, offering enhanced functionality compared to gather()
<a id="h-conclusion"></a>
Conclusion
In this article, I have explained the pivot_longer() function from R's tidyr package, which offers a simple method to reshape data into long format. I also explained how its flexible customization options allow us to create a longer-format data frame by specifying column names and managing missing values.
Happy Learning!
<a id="h-related-articles"></a>
Related Articles
- Explain the tidyr gather() function with examples.
- Explain the R tidyr spread() function.
- Explain the pivot_wider() function in R.
- Explain the fill() function in R.
- Explain the drop_na() function in R.
- Explain the replace_na() function in R.
- Explain the separate() function in R.
- Explain the unite() function in R.
- Explain separate_rows() function in R.
- Explain complete() function in R.
<a id="h-references"></a>
