Open In App

How to Use xtabs() in R to Calculate Frequencies?

Last Updated : 19 Dec, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will be looking at different methods to use xtabs() function to calculate frequencies in the R programming language.

xtabs() function: This function is used to create a contingency table from cross-classifying factors, usually contained in a data frame, using a formula interface.

Syntax: xtabs(formula = ~., data = parent.frame())

Parameters:

  • formula; a formula object with the cross-classifying variables (separated by +) on the right-hand side.
  • data: an optional matrix or data frame containing the variables in the formula. By default, the variables are taken from the environment.

Method 1: Using xtabs() function to calculate one way frequency

In this method to calculate the one-way frequency, the user needs to simply call the xtabs() function and pass it with the required variable to which the frequency is to be calculated and further this will be returning the frequency of the passed single variable to the function in the R programming language.

Example: In this example, we will be taking a data frame of five different variables and then using the xtabs() function we will be calculating the frequency bypassing single the z variable to it in the R language,

R




data <- data.frame(v=rep(c('A', 'B', 'C'), 
                         times=c(20, 16, 14)),
                   w=rep(c('D', 'E', 'F'),
                         times=c(10, 10, 30)),
                   x=rep(c('G', 'H', 'I'),
                         times=c(15, 20, 15)),
                   y=rep(c('J', 'K', 'L'), 
                         times=c(16, 16,18)),
                   z=rep(c('M', 'N', 'O'), 
                         times=c(25, 15,10)))
  
xtabs(~z, data)


Output:

 M  N  O 
25 15 10 

Method 2: Using xtabs() function to calculate two-way frequency

In this method,  the user can get the combined frequency using the xtabs() function by simply calling this function and using the ‘+’ sign at the end of the first variable, and then writing the name of the second variable to get the combined frequency of both the variable passed as the function parameter combine in the R programming language.

Example: In this example, we will be using the same data frame as used in the previous example to get the frequency of the w and y variables together using the ‘+’ sign with the xtabs() function in the R programming language.

R




data <- data.frame(v=rep(c('A', 'B', 'C'), 
                         times=c(20, 16, 14)),
                   w=rep(c('D', 'E', 'F'), 
                         times=c(10, 10, 30)),
                   x=rep(c('G', 'H', 'I'),
                         times=c(15, 20, 15)),
                   y=rep(c('J', 'K', 'L'), 
                         times=c(16, 16,18)),
                   z=rep(c('M', 'N', 'O'),
                         times=c(25, 15,10)))
  
xtabs(~w+y, data)


Output:

   y
w    J  K  L
  D 10  0  0
  E  6  4  0
  F  0 12 18

Method 3: Using xtabs() function to calculate n-way frequency

In this method to get the frequencies using the xtabs() function, the user can get the combined frequencies of the n- variable using this function simply by adding the ‘+’ sign between the variables, and further this function will be returning the frequencies of the variables mentioned in the R programming language.

Example: In this example, we will be calculating the frequencies of all the variables in the data frame using the n way frequency method of the xtabs() function in the R programming language.

R




data <- data.frame(v=rep(c('A', 'B', 'C'),
                         times=c(20, 16, 14)),
                   w=rep(c('D', 'E', 'F'), 
                         times=c(10, 10, 30)),
                   x=rep(c('G', 'H', 'I'),
                         times=c(15, 20, 15)),
                   y=rep(c('J', 'K', 'L'),
                         times=c(16, 16,18)),
                   z=rep(c('M', 'N', 'O'), 
                         times=c(25, 15,10)))
  
xtabs(~v+w+x+y+z, data)


Output:

, , x = G, y = J, z = M

   w
v    D  E  F
  A 10  5  0
  B  0  0  0
  C  0  0  0

, , x = H, y = J, z = M

   w
v    D  E  F
  A  0  1  0
  B  0  0  0
  C  0  0  0

, , x = I, y = J, z = M

   w
v    D  E  F
  A  0  0  0
  B  0  0  0
  C  0  0  0

, , x = G, y = K, z = M

   w
v    D  E  F
  A  0  0  0
  B  0  0  0
  C  0  0  0

, , x = H, y = K, z = M

   w
v    D  E  F
  A  0  4  0
  B  0  0  5
  C  0  0  0

, , x = I, y = K, z = M

   w
v    D  E  F
  A  0  0  0
  B  0  0  0
  C  0  0  0

, , x = G, y = L, z = M

   w
v    D  E  F
  A  0  0  0
  B  0  0  0
  C  0  0  0

, , x = H, y = L, z = M

   w
v    D  E  F
  A  0  0  0
  B  0  0  0
  C  0  0  0

, , x = I, y = L, z = M

   w
v    D  E  F
  A  0  0  0
  B  0  0  0
  C  0  0  0

, , x = G, y = J, z = N

   w
v    D  E  F
  A  0  0  0
  B  0  0  0
  C  0  0  0

, , x = H, y = J, z = N

   w
v    D  E  F
  A  0  0  0
  B  0  0  0
  C  0  0  0

, , x = I, y = J, z = N

   w
v    D  E  F
  A  0  0  0
  B  0  0  0
  C  0  0  0

, , x = G, y = K, z = N

   w
v    D  E  F
  A  0  0  0
  B  0  0  0
  C  0  0  0

, , x = H, y = K, z = N

   w
v    D  E  F
  A  0  0  0
  B  0  0  7
  C  0  0  0

, , x = I, y = K, z = N

   w
v    D  E  F
  A  0  0  0
  B  0  0  0
  C  0  0  0

, , x = G, y = L, z = N

   w
v    D  E  F
  A  0  0  0
  B  0  0  0
  C  0  0  0

, , x = H, y = L, z = N

   w
v    D  E  F
  A  0  0  0
  B  0  0  3
  C  0  0  0

, , x = I, y = L, z = N

   w
v    D  E  F
  A  0  0  0
  B  0  0  1
  C  0  0  4

, , x = G, y = J, z = O

   w
v    D  E  F
  A  0  0  0
  B  0  0  0
  C  0  0  0

, , x = H, y = J, z = O

   w
v    D  E  F
  A  0  0  0
  B  0  0  0
  C  0  0  0

, , x = I, y = J, z = O

   w
v    D  E  F
  A  0  0  0
  B  0  0  0
  C  0  0  0

, , x = G, y = K, z = O

   w
v    D  E  F
  A  0  0  0
  B  0  0  0
  C  0  0  0

, , x = H, y = K, z = O

   w
v    D  E  F
  A  0  0  0
  B  0  0  0
  C  0  0  0

, , x = I, y = K, z = O

   w
v    D  E  F
  A  0  0  0
  B  0  0  0
  C  0  0  0

, , x = G, y = L, z = O

   w
v    D  E  F
  A  0  0  0
  B  0  0  0
  C  0  0  0

, , x = H, y = L, z = O

   w
v    D  E  F
  A  0  0  0
  B  0  0  0
  C  0  0  0

, , x = I, y = L, z = O

   w
v    D  E  F
  A  0  0  0
  B  0  0  0
  C  0  0 10


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads