Open In App

Tidyverse Functions in R

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.

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.

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.

Article Tags :