What are some examples of tables that can be printed in R? 2

What are some examples of tables that can be printed in R?

Tables are an important tool in data analysis and can be easily generated using the statistical programming language R. There are various types of tables that can be printed in R, depending on the type of data and the purpose of analysis. Some common examples include contingency tables, frequency tables, and summary tables. Contingency tables are used to display the relationship between two categorical variables, while frequency tables show the counts or percentages of different categories within a single variable. Summary tables, on the other hand, provide an overview of the data by displaying descriptive statistics such as mean, median, and standard deviation. Other types of tables that can be printed in R include cross-tabulation tables, correlation tables, and regression tables. These tables can be customized and formatted to suit the specific needs of the analysis, making R a versatile and powerful tool for data exploration and presentation.

Print Tables in R (3 Examples)


Often you may want to print a table to the console in R to summarize the values in some dataset.

The following examples show how to print tables in R by using the table() and as.table() functions.

Example 1: Print One-Way Table from Data

Suppose we have the following data frame in R:

#create data frame
df <- data.frame(team=c('A', 'A', 'A', 'B', 'B', 'B', 'C', 'C', 'C'),
                 position=c('Guard', 'Guard', 'Forward', 'Guard', 'Forward',
                            'Forward', 'Guard', 'Guard', 'Forward'),
                 points=c(14, 12, 15, 20, 22, 36, 10, 16, 19))

#view data frame
df

  team position points
1    A    Guard     14
2    A    Guard     12
3    A  Forward     15
4    B    Guard     20
5    B  Forward     22
6    B  Forward     36
7    C    Guard     10
8    C    Guard     16
9    C  Forward     19

We can use the table() function to summarize the count of each unique value in the position column:

#create table for 'position' variable
table1 <- table(df$position)

#view table
table1

Forward   Guard 
      4       5

From the table we can see that ‘Forward’ appears 4 times in the position column and ‘Guard’ appears 5 times.

This is referred to as a one-way table because it summarizes one variable.

Example 2: Print Two-Way Table from Data

Once again suppose we have the following data frame in R:

#create data frame
df <- data.frame(team=c('A', 'A', 'A', 'B', 'B', 'B', 'C', 'C', 'C'),
                 position=c('Guard', 'Guard', 'Forward', 'Guard', 'Forward',
                            'Forward', 'Guard', 'Guard', 'Forward'),
                 points=c(14, 12, 15, 20, 22, 36, 10, 16, 19))

#view data frame
df

  team position points
1    A    Guard     14
2    A    Guard     12
3    A  Forward     15
4    B    Guard     20
5    B  Forward     22
6    B  Forward     36
7    C    Guard     10
8    C    Guard     16
9    C  Forward     19

We can use the table() function to summarize the count of each unique value in the team and position columns:

#create two-way table for 'team' and 'position' variables
table2 <- table(df$team, df$position)

#view table
table2

    Forward Guard
  A       1     2
  B       2     1
  C       1     2

From the table we can see:

  • There is 1 Forward on team A.
  • There are 2 Guards on team A.
  • There are 2 Forwards on team B.

And so on.

Example 3: Print Table From Scratch

Suppose we already know the values that we’d like to fill in a table.

For example, suppose we want to create the following table in R that shows the results of a survey that asked 100 people which sport they liked best:

We can use the as.table() function in R to quickly create this table:

#create matrix
data <- matrix(c(13, 23, 15, 16, 20, 13), ncol=3)

#specify row and column names of matrix
rownames(data) <- c('Male', 'Female')
colnames(data) <- c('Baseball', 'Basketball', 'Football')#convert matrix to table
data <- as.table(data)

#display table
data

       Baseball Basketball Football
Male         13         15       20
Female       23         16       13

The values in the table match the values from the table we saw earlier.

Additional Resources

The following tutorials explain how to perform other common tasks in R:

Cite this article

stats writer (2024). What are some examples of tables that can be printed in R?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/what-are-some-examples-of-tables-that-can-be-printed-in-r/

stats writer. "What are some examples of tables that can be printed in R?." PSYCHOLOGICAL SCALES, 27 Jun. 2024, https://scales.arabpsychology.com/stats/what-are-some-examples-of-tables-that-can-be-printed-in-r/.

stats writer. "What are some examples of tables that can be printed in R?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/what-are-some-examples-of-tables-that-can-be-printed-in-r/.

stats writer (2024) 'What are some examples of tables that can be printed in R?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/what-are-some-examples-of-tables-that-can-be-printed-in-r/.

[1] stats writer, "What are some examples of tables that can be printed in R?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.

stats writer. What are some examples of tables that can be printed in R?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.

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