• Post author:
  • Post category:R Programming
  • Post last modified:August 26, 2024
  • Reading time:12 mins read
You are currently viewing Explain table() Function in R

The table() function in R is used to create the frequency tables also known as contingency tables from R objects containing categorical data. It takes one or more vectors (or factors) as input and returns a table showing the frequency of each unique combination of elements. This function is primarily used in statistics to display the cross-tabulation of multiple categorical variables.

Advertisements

In this article, I will explain the table() function and how to create tables from various R objects such as vectorsdata frames, and factors using its syntax, parameters, and usage.

Key Points-

  • The table() function in R is used to create frequency tables, also known as contingency tables, from categorical data.
  • It accepts one or more vectors, factors, or data frames as input to display the frequency of unique combinations of elements.
  • The function is commonly used in statistics for cross-tabulation of multiple categorical variables.
  • The useNA parameter allows you to manage missing values (NA), either by excluding, including only when present, or always including them as a separate category.
  • You can create simple frequency tables from vectors or more complex contingency tables from data frames.
  • Subsetting tables is possible using square bracket notation, allowing manipulation based on specific rows or columns.
  • The prop.table() function can be applied to frequency or contingency tables to calculate and display the relative frequencies (proportions).

R table() Function

The R base table() function is used to generate a categorical representation of data with the variable name and frequency in the form of a table. In a table, the rows represent the categories of one variable, while the columns represent the categories of another variable. Each cell in the table shows the frequency or count of observations that fall into the corresponding category combination.

Syntax of table() function

Following is the syntax of the table() function.


# Syntax of table() function
table(x, useNA = c("no", "ifany", "always"))

Parameters

  •   x...: One or more vectors, data frames, or factors. Each element contributes to the dimensions of the resulting table.
  • useNA: A parameter to specify how NA values are handled:
  • "no" (default): Excludes NA values.
  • "ifany": Includes NA values only if they are present in the data.
  • "always": Always includes NA as a separate level.

Return value

The table() function returns an object of class table, which is an array with named dimension names (dimnames). This array includes the frequency counts of the combinations of factor levels.

Using table() Function to Create Frequency Table

Using the base R function, you can create a frequency table from a vector. By passing the vector into this function, it returns a frequency table, which is a list of objects displaying the frequency of each item in the table.


# Create frequency table using table() function
# Create a sample vector
vec <- c("A", "B", "C", "D", "E", "F")
print("Given vector:")
print(vec)

# Create a table
print("Create a Frequency Table:")
table_data <- table(vec)
print(table_data)
print("Get the type of object:")
print(class(table_data))

Yields below output.

table in r

Create a Contingency Table using R table() Function

To create a contingency table from a data frame you can pass the columns of the data frame into the table() function. This will return a contingency table where the values represent the frequency of the combinations of the given column values.


# Create a contingency table from data frame
df <- data.frame(
gender = c("Male", "Male", "Female", "Female", "Male", "Female"),
product = c("A", "B", "A", "B", "A", "B")
)
print("Given data frame:")
print(df)

# Create a contingency table
table_data <- table(df$gender, df$product)
print("After creating a table:")
print(table_data)
print("Get the type of object:")
print(class(table_data))

The above code has returned the below contingency table represents the frequency distribution of the gender variable across the product categories A and B.

Yields below output.

create a table in r

Create Tables with NA Values using table()

You can manage NA values when creating a table using the table() function by setting the useNA = "ifany" parameter. This will include NA values in the table. Let’s create a data frame containing NA values and then pass its columns into the table() function to generate a contingency table. This table will show the frequency of each combination of values, including the NA occurrences.


# Handle NA values using useNA = ifany
# Create a data frame
df <- data.frame(
  gender = c("Male", NA, "Female", "Female", "Male", NA),
  product = c("A", "B", "A", "B", "A", "B")
)
print("Given data frame:")
print(df)

# Create a contingency table
table_data <- table(df$gender, df$product, useNA = "ifany")
print("After creating a table:")
print(table_data)
print("Get the type of object:")
print(class(table_data))

Yields below output.


# Output:
[1] "Given data frame:"
  gender product
1   Male       A
2   <NA>       B
3 Female       A
4 Female       B
5   Male       A
6   <NA>       B

[1] "After creating a table:"
        A B
  Female 1 1
  Male   2 0
  <NA>   0 2

[1] "Get the type of object:"
[1] "table"

Subset the Table

You can manipulate a table by subsetting the data based on column or row names. To do this, use square bracket notation ([]) with the table, specifying the rows before the comma and the columns after the comma in R tables.

For example, you can pass the specified column name into the square brackets along with the table to subset the table based on the desired criteria.


# Subset the table
# Create a data frame
df <- data.frame(
  gender = c("Male", "Male", "Female", "Female", "Male", "Female"),
  product = c("A", "B", "A", "B", "A", "B")
)
# Create a contingency table
table_data <- table(df$gender, df$product)
print("Create table:")
print(table_data)
# Subset the table
subset_table <- table_data[,"A"]
print("After subsetting the table:")
print(subset_table)

Yields below output.


 # Output:
Create table       
         A B
  Female 1 2
  Male   2 1

[1] "After subsetting the table:"
Female   Male 
     1      2 

Create Frequency Table of Proportions

To create a frequency table of proportions, you can use the prop.table() function to an existing frequency table. Let’s create a frequency table from a given vector using the table() function and then apply the prop.table() function to calculate the relative frequencies (proportions) of each element. Finally, print both the frequency table and the table of proportions.


# Create Frequency Table of Proportions 
# Create vector
vec <- c("A", "B", "C", "D", "E", "F")

# Create a table
print("Create a Frequency Table:")
table_data <- table(vec)
print(table_data)

relative_freq <- prop.table(table_data)
print("Calculate relative frequencies of frequency table:")
print(relative_freq)

Yields below output.


# Output:
[1] "Create a Frequency Table:"
vec
A B C D E F 
1 1 1 1 1 1 

[1] "Calculate relative frequencies of frequency table:"
 vec
        A         B         C         D         E         F 
 0.1666667 0.1666667 0.1666667 0.1666667 0.1666667 0.1666667 

Create a Contingency Table of Proportions

To create a contingency table of proportions, you can use the prop.table() function to an existing contingency table. Let’s create a contingency table from a given data frame using the table() function and then apply the prop.table() function to calculate the relative frequencies (proportions) of each combination. Finally, print both the contingency table and the table of proportions.


# Create frequency table of proportions from data frame
# Create data frame
df <- data.frame(
gender = c("Male", "Male", "Female", "Female", "Male", "Female"),
product = c("A", "B", "A", "B", "A", "B")
)
# Create a contingency table
table_data <- table(df)
print("Create contingency table:")
print(table_data)

relative_freq <- prop.table(table_data)
print("Calculate relative frequencies of contingency table:")
print(relative_freq)

Yields below output.


# Output:
[1] "Create contingency table:"
        product
gender   A B
  Female 1 2
  Male   2 1

[1] "Calculate relative frequencies of contingency table:"
        product
gender           A         B
  Female 0.1666667 0.3333333
  Male   0.3333333 0.1666667

Conclusion

In this article, I have explained the table() function in R is a powerful tool for creating and analyzing categorical data from various R objects such as vectors, data frames, and factors. Additionally, I explained how to calculate the proportions of these tables using the prop.table() function.

Happy learning!!

References