Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Data Visualization
  • Statistics in R
  • Machine Learning in R
  • Data Science in R
  • Packages in R
  • Data Types
  • String
  • Array
  • Vector
  • Lists
  • Matrices
  • Oops in R
Open In App
Next Article:
Tidyverse Functions in R
Next article icon

Tidyverse Functions in R

Last Updated : 17 Apr, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Tidyverse is a collection of R packages designed to make data analysis easier, more intuitive, and efficient. Among the various packages within Tidyverse, several key functions stand out for their versatility and usefulness in data manipulation tasks. In this article, we'll explore some of the most essential Tidyverse functions in R Programming Language.

Essential Tidyverse Functions

  1. filter(): Used to filter rows based on specified conditions. Allows for selecting rows that meet specific criteria.
  2. select(): Enables selecting columns based on column names or conditions. Useful for extracting relevant columns from a dataset.
  3. mutate(): Adds new variables or modifies existing ones based on defined transformations. Allows for creating calculated columns.
  4. group_by(): Groups data by one or more variables. Typically used in conjunction with aggregation functions for summarizing grouped data.
  5. summarize(): Computes summary statistics for groups of data. Often combined with group_by() to generate group-level summaries.
  6. arrange(): Sorts rows of a dataset based on specified column(s). Allows for arranging data in ascending or descending order.
  7. join() family (inner_join(), left_join()): Functions for merging datasets based on common columns. Provides flexibility in combining datasets using different types of joins.
  8. pivot_longer() and pivot_wider(): Reshape data between long and wide formats. Useful for converting between different data representations.

Let's demonstrate the usage of some of these essential Tidyverse functions with examples.

R
library(dplyr) # Load sample dataset data <- tibble(   id = 1:5,   category = c("A", "B", "A", "C", "B"),   value = c(10, 15, 20, 25, 30) ) data  # Example: Filtering rows filtered_data <- filter(data, value > 15) filtered_data  # Example: Selecting columns selected_data <- select(data, id, value) selected_data  # Example: Creating a new variable mutated_data <- mutate(data, value_squared = value^2) mutated_data   # Example: Grouping data and summarizing grouped_summary <- data %>%   group_by(category) %>%   summarize(avg_value = mean(value))  grouped_summary # Example: Sorting data sorted_data <- arrange(data, desc(value)) sorted_data  # Example: Joining datasets df1 <- tibble(id = c(1, 2, 3), value = c("A", "B", "C")) df2 <- tibble(id = c(2, 3, 4), attribute = c("X", "Y", "Z")) joined_data <- inner_join(df1, df2, by = "id") joined_data 

Output:

# A tibble: 5 × 3
id category value
<int> <chr> <dbl>
1 1 A 10
2 2 B 15
3 3 A 20
4 4 C 25
5 5 B 30

filtered_data
# A tibble: 3 × 3
id category value
<int> <chr> <dbl>
1 3 A 20
2 4 C 25
3 5 B 30

selected_data
# A tibble: 5 × 2
id value
<int> <dbl>
1 1 10
2 2 15
3 3 20
4 4 25
5 5 30

mutated_data
# A tibble: 5 × 4
id category value value_squared
<int> <chr> <dbl> <dbl>
1 1 A 10 100
2 2 B 15 225
3 3 A 20 400
4 4 C 25 625
5 5 B 30 900

grouped_summary
# A tibble: 3 × 2
category avg_value
<chr> <dbl>
1 A 15
2 B 22.5
3 C 25

sorted_data
# A tibble: 5 × 3
id category value
<int> <chr> <dbl>
1 5 B 30
2 4 C 25
3 3 A 20
4 2 B 15
5 1 A 10

joined_data
# A tibble: 2 × 3
id value attribute
<dbl> <chr> <chr>
1 2 B X
2 3 C Y

Reshaping data from wide to long format.

R
library(tidyr) # Example: Reshaping data from wide to long format wide_data <- tibble(   id = 1:3,   var1_2019 = c(10, 20, 30),   var1_2020 = c(15, 25, 35),   var2_2019 = c(100, 200, 300),   var2_2020 = c(150, 250, 350) ) wide_data long_data <- pivot_longer(wide_data, cols = starts_with("var"), names_to = "year",                            values_to = "value")  long_data # Example: Reshaping data from long to wide format wide_data <- pivot_wider(long_data, names_from = year, values_from = value) wide_data  

Output:

# A tibble: 3 × 5
id var1_2019 var1_2020 var2_2019 var2_2020
<int> <dbl> <dbl> <dbl> <dbl>
1 1 10 15 100 150
2 2 20 25 200 250
3 3 30 35 300 350

# A tibble: 12 × 3
id year value
<int> <chr> <dbl>
1 1 var1_2019 10
2 1 var1_2020 15
3 1 var2_2019 100
4 1 var2_2020 150
5 2 var1_2019 20
6 2 var1_2020 25
7 2 var2_2019 200
8 2 var2_2020 250
9 3 var1_2019 30
10 3 var1_2020 35
11 3 var2_2019 300
12 3 var2_2020 350

# A tibble: 3 × 5
id var1_2019 var1_2020 var2_2019 var2_2020
<int> <dbl> <dbl> <dbl> <dbl>
1 1 10 15 100 150
2 2 20 25 200 250
3 3 30 35 300 350

Conclusion

Tidyverse functions provide a comprehensive toolkit for data manipulation tasks in R. By leveraging functions like filter(), mutate(), group_by(), and others, you can efficiently clean, transform, and analyze datasets. Whether you need to filter rows, calculate summary statistics, reshape data, or perform complex joins, Tidyverse functions offer a consistent and intuitive syntax for achieving your data analysis goals. With practice and exploration, you can become proficient in using Tidyverse functions to tackle a wide range of data manipulation challenges in R.


Next Article
Tidyverse Functions in R

M

manojjai07nu
Improve
Article Tags :
  • R Language
  • R Dplyr

Similar Reads

    Tidyverse joins in R
    Data manipulation is a crucial aspect of data analysis and plays a significant role in deriving insights from datasets. The Tidyverse package in R provides a suite of tools for data manipulation, including powerful functions for joining datasets. In this article, we'll explore Tidyverse joins, which
    3 min read
    View Function in R
    The View() function in R is a built-in function that allows users to view the contents of data structures interactively in a spreadsheet-like format. When we use the View() function, it opens a separate window or tab (depending on your R environment) displaying the data in a table format, making it
    2 min read
    sum() function in R
    sum() function in R Programming Language returns the addition of the values passed as arguments to the function. Syntax: sum(...) Parameters: ...: numeric or complex or logical vectorssum() Function in R ExampleR program to add two numbersHere we will use sum() functions to add two numbers. R a1=c(1
    2 min read
    Interactive() Function in R
    In this article, we are going to see interactive() Function in R Programming Language. Interactive() Function  It returns TRUE when R is being used interactively and FALSE otherwise. This function is used to test whether R runs interactively or not. Syntax: interactive()   It will return TRUE, if is
    1 min read
    Outer() Function in R
    A flexible tool for working with matrices and vectors in R is the outer() function. It enables you to create a new matrix or array by applying a function to every conceivable combination of the items from two input vectors. outer() function in R Programming Language is used to apply a function to tw
    3 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences