Built with Tutify.io

    AIStudyLabs

    Explain table() Function in R

    Explain table() Function in R

    The table() function in R is used to create the frequency tablesalso 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.

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

    Key Points-

    <a id="h-r-table-function"></a>

    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.

    <a id="h-syntax-of-table-function"></a>

    Syntax of table() function

    Following is the syntax of the table() function.

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

    <a id="h-parameters"></a>

    Parameters

    <a id="h-return-value"></a>

    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.

    <a id="h-using-table-function-to-create-frequency-table"></a>

    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.

    <a id="h-create-a-contingency-table-using-r-table-function"></a>

    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.

    <a id="h-create-tables-with-na-values-using-table"></a>

    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          B
    3 Female       A
    4 Female       B
    5   Male       A
    6          B
    
    [1] "After creating a table:"
            A B
      Female 1 1
      Male   2 0
         0 2
    
    [1] "Get the type of object:"
    [1] "table"
    

    <a id="h-subset-the-table"></a>

    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
    

    <a id="h-create-frequency-table-of-proportions"></a>

    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
    

    <a id="h-create-a-contingency-table-of-proportions"></a>

    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
    

    <a id="h-conclusion"></a>

    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!!

    <a id="h-related-articles"></a>

    Related Articles

    <a id="h-references"></a>

    References