How to Create a Table in R?

You are currently viewing How to Create a Table in R?

How to create a table in R? You can use the R base table() function to create a contingency table from various R objects such as vectors, data frames, and matrices. A contingency table is nothing but a cross-tabular data structure that displays the frequency of different combinations of factor levels, offering insights into the distribution and relationships within the data. In this article, I will discuss how to use the table() function and explore other functions and packages that can help create and manipulate tables in R.

Advertisements

Key points-

R Base table() Function

The table() function in R language is used to generate a categorical representation of data with the variable name and frequency in the form of a table. This function creates contingency tables, which count the combinations of factor levels. It is straightforward and effective for creating tables from vectors, factors, and data frames to produce cross-tabulations of categorical data.

Syntax of table()

Following is the syntax of the table() function.

 # Syntax of table() function table(x) 

Parameters

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.

Create a Frequency Table using table()

You can create a frequency table from a vector using the base R table() function. 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 a table using table() # Create a sample vector print("Given vector:") data  

Yields below output.

create a table in r

Create a Contingency Table From a Data Frame

To create a contingency table from a data frame, 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  

Yields below output.

create a table in r

As shown in the output above, the R table has been created from the data frame. In this table, the column names correspond to the unique values from the second column of the data frame, while the row names represent the unique values from the first column. The values within the table indicate the counts of each combination of gender and product .

Create Tables with NA Values

When creating tables, use the use NA = "ifany" parameter to handle NA values and include them in the table. Let’s create a data frame with NA values and pass its column values into the table() function to get a contingency table where the values represent the frequency of the combination of column values, including the occurrences of NA values.

 # Create a data frame df  

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" 

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  

Yields below output.

 # Output: Create table A B Female 1 2 Male 2 1 [1] "After subsetting the table:" Female Male 1 2 

Convert Table to Data Frame

Alternatively, you can convert R tables into a data frame using the as.data.frame() function. Simply pass the table into the as.data.frame() function, which will transform the table object into a data frame, including one of the columns for frequency values.

 # Convert table to data frame df1  

Yields below output.

 # Output: [1] "After converting the table to data frame:" Var1 Var2 Freq 1 Female A 1 2 Male A 2 3 Female B 2 4 Male B 1 [1] "Get the type of object:" [1] "data.frame" 

Create a Table using the xtabs Function

Similarly, you can use the base R xtabs() function to create contingency tables from data frames. By passing the columns of the data frame along with the data frame itself into this function, it will generate a table where the values represent the frequency of the combinations of the specified columns.

 # Create table using xtabs() table_data  

Yields below output.

 # Output: [1] "After creating a table:" product gender A B Female 1 2 Male 2 1 [1] "Get the type of object:" [1] "xtabs" "table" 

Using the tidyverse to Create a Contingency Table in R

Use the tidyverse package to create and manipulate tables. Before using these functions, you need to install and load the package into your R environment. You can then use the %>% , count() , and pivot_wider() functions to generate tables from a given data frame.

 # Create table using tidyverse package # Install and load tidyverse package library(tidyverse) # Create a data frame df % count(gender, product) %>% pivot_wider(names_from = gender, values_from = n, values_fill = list(n = 0)) print("After creating a table:") print(table_data) print("Get the type of object:") print(class(table_data)) 

Yields below output.

 # Output: [1] "After creating a table:" # A tibble: 2 × 3 product Female Male   1 A 1 2 2 B 2 1 [1] "Get the type of object:" [1] "tbl_df" "tbl" "data.frame" 

Using as.table() function to Create Tables

Finally, you can use the as.table() function to create tables from matrices. Passing a matrix into this function will generate a table where the values are the same as those in the given matrix.

 # Create table using as.table() function # Create matrix data  

Yields below output.

 # Output: [1] "Create table:" A B C D F 1 2 3 4 G 5 6 7 8 [1] "Get the type:" [1] "table" 

Conclusion

In this article, I have explained the table() function in R, including its syntax, parameters, and usage for creating and manipulating contingency tables from R objects like vectors, data frames, and matrices. Additionally, I have covered how to use other functions like xtabs() , as.table() , and the tidyverse package to generate contingency tables from R objects.

Related Articles