To create pivot tables in R, you can use group_by() and summarise() function from dplyr package.

The following method shows how you can do it with syntax.

Method 1: Use dplyr Package

df %>%
  group_by(group1,group2) %>% 
  summarize(sum = sum(values), mean = mean(values))

The following example shows how to create pivot table in R using dplyr package.

Using dplyr Package

Let’s see how we can use group_by() and summarise() function from dplyr package in R:

# Load library
library(dplyr)

# Create data frame
df <- data.frame(Machine_name=c("A","B","C","D","E","F","G","H"),
                  Pressure=c(12.39,11.25,12.15,13.48,13.78,12.89,12.21,12.58),
                  Temperature=c(78,89,85,84,81,79,77,85),
                  Status=c(TRUE,TRUE,FALSE,TRUE,FALSE,FALSE,TRUE,FALSE))

# Create pivot table                  
df %>%
  group_by(Status) %>% 
  summarize(sum = sum(Temperature), mean = mean(Temperature))

Output:

  Status   sum  mean
  <lgl>  <dbl> <dbl>
1 FALSE    330  82.5
2 TRUE     328  82 

Here the output shows pivot table which having sum and mean value Temperature column group by Status column of dataframe.