table in R: Create a Tabular representation of Data in R

Among the various ways to represent data in R, the most commonly used is a table.

table in R

A table is a built-in R data structure used to represent data using variable names and frequency categorically. The table uses the cross-classifying factors to build a contingency table of the counts at each combination of factor levels.




The obj is the object to be converted to tabular form.


table in r diagram

  1. The table in R can also represent tabular data based on conditions. 
  2. The table in R can also be used for cross-tabulations.
  3. A frequency table with proportions can be created in R using a prop.table().
  4. Data with a table in R can also be represented in three-dimensional form.

Advantages of a table in R

  1. Data can be represented categorically based on frequency.
  2. Data can be represented categorically based on proportions.
  3. Data can be represented categorically based on conditions.
  4. Data can be represented in a cross-tabular form.
  5. Data can be represented in more than two dimensions.
  6. The data prepared using tables can serve as the base for further graphical representations and plots.

Shortcomings of a table in R

  1. A complete understanding of the data might not always be possible with a table alone. Further graphical plots might be needed to understand or visualize complex data.
  2. Not every kind of data can be represented in the form of a table with frequency.
  3. This representation is a basic form of data representation in R.

Examples of the table in R

Example 1:

The following example gathers the data for cylinders from the mtcars dataset (32 rows) and presents it in terms of frequency using a table in R

We observe 32 observations recorded for the column cyl in the mtcars dataset, and we intend to present the frequency of these observations in a tabular form. Note that in the final output table, we know that there are  11 observations of cars with a 4-cylinder engine, 7 observations of cars with a 6-cylinder engine, and 14 observations of cars with an 8-cylinder engine.

cylinder <- table(mtcars$cyl)


example Output

Example 2

Consider the situation where an event manager has to have a reference for the gender of their guests. To create a data frame in R, use the data.frame() function.

The data in this situation can be represented in a tabular form as follows:

guestList <- data.frame(
 "Name" = c("Sam", "Julie", "Rob"),
 "Gender" = c("Male", "Female", "Male")
guestListTable <- table(guestList)



Name  Female  Male
Julie   1     0
Rob     0     1
Sam     0     1

Example 3

In the previous example, the representation of data can be further extended to identify which of the guests on the guest list are vegan so that the event management team can cater to their dining needs accordingly. This data can be represented with the help of the table shown below.

Here, observe that R splits this data based on the values of the Vegan field and represents this data in a three-dimensional form.

guestList <- data.frame(
 "Name" = c("Sam", "Julie", "Rob"),
 "Gender" = c("Male", "Female", "Male"),
 "Vegan" = c("Yes", "Yes", "No")

guestListTable <- table(guestList)


Example 3

That’s it for this tutorial.

Related posts

How to Create Contingency Table in R

kable in R

Leave a Comment