Tidyr package is used to create tidy data. Tidy data is a standard way of storing data that is used to ensure that we spend less time fighting with the tools and more time working on the analysis. The different functions of Tidyr package are described here.
Problem:
Use of Tidyr Separate() in R
Solution:
Separate() is used to split a single variable into two or more variables. In some scenarios, a single column captures multiple variables and we may need to separate the values into 2 or more variables or we may need to exclude the unnecessary part of the variable and Separate() does the same.
We can use the “flight_unite” dataset that we had created before in the Tidyr::Unite example:
![]() |
flight_unite |
Below is the code and output to Separate the date column into “month”, “day” and “year” as before:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(nycflights13) | |
library(tidyr) | |
# Check the Dataset flight_unite | |
View(flight_unite) | |
#----------------------------------------------------------------- | |
# Separate Date column into year, month and day | |
flight_separate <- separate(flight_unite, date, c('month','day','year')) | |
View(flight_separate) |
![]() |
flight_separate |