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

# Create data frame
df <- data.frame(Machine_name=c("A","B","C","D","E","F","G","H"),

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


  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.