How to Create Pivot Tables in R?
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.
df %>% group_by( grouping_variables) %>% summarize( label = aggregate_fun() )
- 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
# A tibble: 3 x 2 label sum_values <chr> <dbl> 1 Geek1 374 2 Geek2 160 3 Geek3 104
Example 2: Create pivot table
# A tibble: 3 x 2 label average_values <chr> <dbl> 1 Geek1 125. 2 Geek2 53.3 3 Geek3 34.7