To create frequency table in R, you can use two different ways one is using table() function and other is using dplyr package.
The followings methods show how you can do it with syntax.
Method 1: Use table() Function
table(vector)
Method 2: Use dplyr Package
library(dplyr)
result <- df %>%
group_by(data) %>%
summarise(count = n())
The following examples show how to create frequency table in R.
Create Frequency Table Using table() Function
Let’s see how we can use table() function to create frequency table in R:
# Create data frame
df <- data.frame(Machine_name=c("A","B","C","D","E","F","G","H","A","B","A","C","D","B","E","H"),
Pressure=c(12.39,11.25,12.15,13.48,13.78,9.63,12.21,12.58,9.6,8.85,7.89,8.50,12.36,11.45,9.47,8.12),
Status=c("OK","Suspect","OK","OK","Suspect","Suspect","Suspect","OK","OK","OK","OK","OK","Suspect","Suspect","Suspect","OK"))
# Create frequency table
f <- table(df$Machine_name)
# Show frequency table
print(f)
Output:
A B C D E F G H
3 3 2 2 2 1 1 2
Here the output shows frequency table for Machine_name column of data frame.
Create Frequency Table Using dplyr Package
Let’s see how we can use dplyr package in R to create frequency table:
library(dplyr)
# Create data frame
df <- data.frame(Machine_name=c("A","B","C","D","E","F","G","H","A","B","A","C","D","B","E","H"),
Pressure=c(12.39,11.25,12.15,13.48,13.78,9.63,12.21,12.58,9.6,8.85,7.89,8.50,12.36,11.45,9.47,8.12),
Status=c("OK","Suspect","OK","OK","Suspect","Suspect","Suspect","OK","OK","OK","OK","OK","Suspect","Suspect","Suspect","OK"))
# Create frequncy table
f <- df %>%
group_by(Status) %>%
summarise(count = n())
# Show frequency table
print(f)
Output:
Status count
<chr> <int>
1 OK 9
2 Suspect 7
Here the output shows frequency table for Status column of data frame which created using dplyr package.