How can I use the Table function in R?

How can I use the Table function in R?

The Table function in R is a useful tool that allows users to create a tabular representation of data in a structured and organized manner. By using this function, users can easily summarize and analyze their data by counting the frequency of occurrences or grouping data into categories. The Table function is particularly helpful for data analysis and visualization, as it provides a clear and concise overview of the data. To use the Table function, users need to input the data they want to analyze and specify the variables they want to include in the table. This function is highly customizable, allowing users to add labels and titles to their tables, as well as perform various calculations and manipulations on the data. In summary, the Table function in R is a powerful tool that simplifies the process of data analysis and presentation.

Use the Table Function in R (With Examples)


The table() function in R can be used to quickly create frequency tables.

This tutorial provides examples of how to use this function with the following data frame in R:

#create data frame
df <- data.frame(player = c('AJ', 'Bob', 'Chad', 'Dan', 'Eric', 'Frank'),
                 position = c('A', 'B', 'B', 'B', 'B', 'A'),
                 points = c(1, 2, 2, 1, 0, 0))

#view data frame
df

  player position points
1     AJ        A      1
2    Bob        B      2
3   Chad        B      2
4    Dan        B      1
5   Eric        B      0
6  Frank        A      0

Example 1: Frequency Table for One Variable

The following code shows how to create a frequency table for the position variable in our data frame:

#calculate frequency table for position variable
table(df$position)

A B 
2 4

From the output we can observe:

  • 2 players in the data frame have a position of ‘A
  • 4 players in the data frame have a position of ‘B

Example 2: Frequency Table of Proportions for One Variable

The following code shows how to use prop.table() to create a frequency table of proportions for the position variable in our data frame:

#calculate frequency table of proportions for position variable
prop.table(table(df$position))

        A         B 
0.3333333 0.6666667

From the output we can observe:

  • 33.33% of players in the data frame have a position of ‘A
  • 66.67% of players in the data frame have a position of ‘B

Note that in a proportion table the sum of the proportions will always be equal to 1.

Example 3: Frequency Table for Two Variables

The following code shows how to create a frequency table for the position and points variable in our data frame:

#calculate frequency table for position and points variable
table(df$position, df$points)

    0 1 2
  A 1 1 0
  B 1 1 2
  • 1 player in the data frame has a position of ‘A‘ and 0 points
  • 1 player in the data frame has a position of ‘A‘ and 1 point
  • 0 players in the data frame have a position of ‘A‘ and 2 points
  • 1 player in the data frame has a position of ‘B‘ and 0 points
  • 1 player in the data frame has a position of ‘B‘ and 1 point
  • 2 players in the data frame have a position of ‘B‘ and 2 points

Example 4: Frequency Table of Proportions for Two Variables

The following code shows how to create a frequency table of proportions for the position and points variable in our data frame:

#calculate frequency table of proportions for position and points variable
prop.table(table(df$position, df$points))

            0         1         2
  A 0.1666667 0.1666667 0.0000000
  B 0.1666667 0.1666667 0.3333333

From the output we can observe:

  • 16.67% of players in the data frame have a position of ‘A‘ and 0 points
  • 16.67% of players in the data frame have a position of ‘A‘ and 1 point
  • 0% of players in the data frame have a position of ‘A‘ and 2 points
  • 16.67% of players in the data frame have a position of ‘B‘ and 0 points
  • 16.67% of players in the data frame have a position of ‘B‘ and 1 point
  • 33.3% of players in the data frame have a position of ‘B‘ and 2 points

Note that we can also use the options() function to specify how many decimals to show in the proportion table:

#only display two decimal places
options(digits=2)

#calculate frequency table of proportions for position and points variable
prop.table(table(df$position, df$points))

       0    1    2
  A 0.17 0.17 0.00
  B 0.17 0.17 0.33

Cite this article

stats writer (2024). How can I use the Table function in R?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-use-the-table-function-in-r/

stats writer. "How can I use the Table function in R?." PSYCHOLOGICAL SCALES, 1 May. 2024, https://scales.arabpsychology.com/stats/how-can-i-use-the-table-function-in-r/.

stats writer. "How can I use the Table function in R?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-use-the-table-function-in-r/.

stats writer (2024) 'How can I use the Table function in R?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-use-the-table-function-in-r/.

[1] stats writer, "How can I use the Table function in R?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, May, 2024.

stats writer. How can I use the Table function in R?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.

Download Post (.PDF)
Slide Up
x
PDF
Scroll to Top