How can tables be created in R? Can you provide examples?

Tables can be created in R using the function “table()” which takes in one or more vectors as input and produces a contingency table as output. A contingency table is a tabular representation of the count or frequency of observations falling into different categories. It is commonly used for displaying and analyzing categorical data.

For example, let’s say we have a vector of gender with values “male” and “female” and a vector of eye color with values “blue”, “brown”, and “green”. We can use the table() function to create a contingency table showing the count of individuals with each combination of gender and eye color:

table(gender, eye_color)

This will result in a table with rows representing gender and columns representing eye color, with the counts in each cell.

Furthermore, tables can also be created using the “xtabs()” function, which allows for more complex tabulations involving multiple variables. It takes in a formula as input, with the left side representing the response variable and the right side representing the explanatory variables.

For example, let’s say we have a dataset with information on the number of hours spent studying, the type of school attended, and the grades received. We can use xtabs() to create a table showing the average number of hours spent studying for each combination of school type and grades:

xtabs(num_hours ~ school_type + grades, data = dataset)

This will result in a table with rows representing school type and columns representing grades, with the average number of hours in each cell.

In summary, tables can be easily created in R using the table() or xtabs() functions, providing a useful tool for analyzing and summarizing categorical data.

Create Tables in R (With Examples)


There are two ways to quickly create tables in R:

Method 1: Create a table from existing data.

tab <- table(df$row_variable, df$column_variable)

Method 2: Create a table from scratch.

tab <- matrix(c(7, 5, 14, 19, 3, 2, 17, 6, 12), ncol=3, byrow=TRUE)
colnames(tab) <- c('colName1','colName2','colName3')
rownames(tab) <- c('rowName1','rowName2','rowName3')
tab <- as.table(tab)

This tutorial shows an example of how to create a table using each of these methods.

Create a Table from Existing Data

The following code shows how to create a table from existing data:

#make this example reproducible
set.seed(1)

#define data
df <- data.frame(team=rep(c('A', 'B', 'C', 'D'), each=4),
                 pos=rep(c('G', 'F'), times=8),
                 points=round(runif(16, 4, 20),0))

#view head of data 
head(df)

  team pos points
1    A   G      8
2    A   F     10
3    A   G     13
4    A   F     19
5    B   G      7
6    B   F     18

#create table with 'position' as rows and 'team' as columns
tab1 <- table(df$pos, df$team)
tab1

  A B C D
F 2 2 2 2
G 2 2 2 2

This table displays the frequencies for each combination of team and position. For example:

  • 2 players are on position ‘F’ on team ‘A’
  • 2 players are on position ‘G’ on team ‘A’
  • 2 players are on position ‘F’ on team ‘B’
  • 2 players are on position ‘G’ on team ‘B’

And so on.

Create a Table from Scratch

The following code shows how to create a table with 4 columns a 2 rows from scratch:

#create matrix with 4 columns
tab <- matrix(rep(2, times=8), ncol=4, byrow=TRUE)

#define column names and row names of matrix
colnames(tab) <- c('A', 'B', 'C', 'D')
rownames(tab) <- c('F', 'G')

#convert matrix to table 
tab <- as.table(tab)

#view table 
tab

  A B C D
F 2 2 2 2
G 2 2 2 2

Notice that this table is the exact same as the one created in the previous example.

Additional Resources

How to Loop Through Column Names in R
How to Create an Empty Data Frame in R
How to Append Rows to a Data Frame in R

x