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.
Syntax
table(obj)
Parameters
The obj is the object to be converted to tabular form.
Diagram
- The table in R can also represent tabular data based on conditions.
- The table in R can also be used for cross-tabulations.
- A frequency table with proportions can be created in R using a prop.table().
- Data with a table in R can also be represented in three-dimensional form.
Advantages of a table in R
- Data can be represented categorically based on frequency.
- Data can be represented categorically based on proportions.
- Data can be represented categorically based on conditions.
- Data can be represented in a cross-tabular form.
- Data can be represented in more than two dimensions.
- The data prepared using tables can serve as the base for further graphical representations and plots.
Shortcomings of a table in R
- 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.
- Not every kind of data can be represented in the form of a table with frequency.
- 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.
library(datasets)
mtcars
cylinder <- table(mtcars$cyl)
cylinder
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)
guestListTable
Output
Gender
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)
guestListTable
Output
That’s it for this tutorial.
Related posts
How to Create Contingency Table in R

Krunal Lathiya is an Information Technology Engineer by education and web developer by profession. He has worked with many back-end platforms, including Node.js, PHP, and Python. In addition, Krunal has excellent knowledge of Data Science and Machine Learning, and he is an expert in R Language. Krunal has written many programming blogs, which showcases his vast expertise in this field.