# Frequency Table in R | 3 Different Methods with Examples

Frequency tables are a fundamental tool in data analysis, helping us gain insights into the distribution of data values. In the R programming language, creating frequency tables is an essential skill for anyone working with data. In this article, we will dive into what frequency tables are, how to create them in R, and explore some advanced techniques using the dplyr package.

## What is a Frequency Table in R?

A frequency table, also known as a frequency distribution table, is a summary of data that displays the number of times each unique value occurs in a dataset. It helps us understand the distribution and patterns within the data, making it an essential tool for exploratory data analysis.

In R, frequency tables are typically represented in a tabular format with two columns: one for unique values and the other for their corresponding frequencies. The process of creating a frequency table involves counting the occurrences of each unique value within a dataset.

## How to Make a Frequency Table in R

Let us now explore different methods to make frequency tables in R.

### Method 1: Using inbuilt R Functionality

Creating a frequency table in R can be achieved using inbuilt R functions. Here's a step-by-step guide on how to do it:

2. Create a Vector: Assuming your data is stored in a data frame, extract the column of interest and create a vector. For example, if you have a data frame df with a column named values, you can create a vector as follows:

```data_vector <- df\$values
```

3. Generate the Frequency Table: Use the table() function to create a frequency table from the vector:

```freq_table <- table(data_vector)
```

4. View the Frequency Table: To view the resulting frequency table, simply print it:

```print(freq_table)
```

### Method 2: Using RStudio

RStudio is a popular integrated development environment (IDE) for R that simplifies the process of working with R and creating frequency tables. Here's how to create a frequency table in RStudio:

2. Create a Vector: Use the same method as before to create a vector from your dataset.

3. Generate the Frequency Table: In RStudio's console or script editor, type the following code:

```freq_table <- table(data_vector)
```

4. View the Frequency Table: To view the frequency table, simply type freq_table and press Enter.

Out third method is using the dplyr package. The dplyr package is a powerful library in R that makes data manipulation tasks, including creating frequency tables, more intuitive and efficient. Let's explore how to create frequency tables using dplyr:

If you haven't already installed the dplyr package, you can do so with the following command:

```install.packages("dplyr")
```

```library(dplyr)
```

### Create a Frequency Table with dplyr

Assuming you have a dataset in a data frame, here's how to create a frequency table using dplyr:

2. Use dplyr Functions: With dplyr, you can create a frequency table and arrange it in a neat format in just a few steps. Assuming your data frame is named df and the variable of interest is values, here's how you can do it:

```freq_table <- df %>%
group_by(values) %>%
summarise(frequency = n()) %>%
arrange(desc(frequency))
```

In this code:

• group_by(values) groups the data by unique values in the values column.
• summarise(frequency = n()) counts the frequency of each unique value.
• arrange(desc(frequency)) arranges the results in descending order of frequency.

View the Frequency Table: To view the frequency table, simply print the freq_table object:

```print(freq_table)
```

### Example

Here's a small example of how to do it using base R.

```# Create a dataset of car colors
car_colors <- c("Red", "Blue", "Green", "Red", "Red", "Blue", "Green", "Yellow", "Blue", "Red")

# Use the table() function to create the frequency table
freq_table <- table(car_colors)

# Display the frequency table
print(freq_table)
```

Output:

```car_colors
Blue   Green    Red Yellow
3      2      4      1
```

## Conclusion

Frequency tables are indispensable for understanding the distribution of data in R. Whether you prefer using base R or the dplyr package, you now have the tools to create and analyze frequency tables. The choice between these methods depends on your familiarity with R and the complexity of your data manipulation needs. Start exploring your data today with frequency tables to unlock valuable insights for your data analysis projects.