Explain the use of the table() function.

This function is used to create the frequency table in R.

In R, the table() function is used to create frequency tables, which show the distribution of categorical variables. It takes one or more categorical variables as input and returns a table of counts for each combination of the variable(s).

Here’s a basic explanation of how to use the table() function:
# Example 1: Create a frequency table for a single categorical variable
data <- c(“A”, “B”, “A”, “C”, “B”, “A”, “A”, “C”, “C”, “B”)
freq_table <- table(data)
print(freq_table)

# Example 2: Create a two-way frequency table for two categorical variables
variable1 <- c(“A”, “B”, “A”, “C”, “B”, “A”, “A”, “C”, “C”, “B”)
variable2 <- c(“X”, “Y”, “X”, “Y”, “Z”, “X”, “Y”, “Z”, “X”, “Z”)
two_way_table <- table(variable1, variable2)
print(two_way_table)
In the first example, a frequency table is created for a single categorical variable (data). The resulting table will show the counts of each unique value in the variable.

In the second example, a two-way frequency table is created for two categorical variables (variable1 and variable2). This table will show the counts for each combination of the two variables.

The table() function is particularly useful for exploring and summarizing the distribution of categorical data in a dataset. It helps in understanding the patterns and relationships between different categorical variables.