Open In App

How to Create Pivot Tables in R?

Last Updated : 19 Dec, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will discuss how to create the pivot table in the R Programming Language.

The Pivot table is one of Microsoft Excel’s most powerful features that let us extract the significance from a large and detailed data set. A Pivot Table often shows some statistical value about the dataset by grouping some values from a column together, To do so in the R programming Language, we use the group_by() and the summarize() function of the dplyr package library. The dplyr package in the R Programming Language is a structure of data manipulation that provides a uniform set of verbs that help us in preprocessing large data. The group_by() function groups the data using one or more variables and then summarize function creates the summary of data by those groups using aggregate function passed to it.

Syntax:

df %>% group_by( grouping_variables) %>% summarize( label = aggregate_fun() )

Parameter:

  • df: determines the data frame in use.
  • grouping_variables: determine the variable used to group data.
  • aggregate_fun(): determines the function used for summary. for example, sum, mean, etc.

Example 1: Create pivot tables

R




# create sample data frame
sample_data <- data.frame(label=c('Geek1', 'Geek2', 'Geek3', 'Geek1'
                                  'Geek2', 'Geek3', 'Geek1', 'Geek2',
                                  'Geek3'),
                             value=c(222, 18, 51, 52, 44, 19, 100, 98, 34))
  
# load library dplyr
library(dplyr)
  
# create pivot table with sum of value as summary
sample_data %>% group_by(label) %>% 
summarize(sum_values = sum(value))


Output:

# A tibble: 3 x 2
 label sum_values
 <chr>      <dbl>
1 Geek1        374
2 Geek2        160
3 Geek3        104

Example 2: Create pivot table

R




# create sample data frame
sample_data <- data.frame(label=c('Geek1', 'Geek2', 'Geek3', 'Geek1'
                                  'Geek2', 'Geek3', 'Geek1', 'Geek2',
                                  'Geek3'),
                             value=c(222, 18, 51, 52, 44, 19, 100, 98, 34))
  
# load library dplyr
library(dplyr)
  
# create pivot table with sum of value as summary
sample_data %>% group_by(label) %>% summarize(average_values = mean(value))


Output:

# A tibble: 3 x 2
 label average_values
 <chr>          <dbl>
1 Geek1          125.  
2 Geek2           53.3
3 Geek3           34.7


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads