How to Use xtabs() in R to Calculate Frequencies? - GeeksforGeeks (2024)

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:

 yw 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 wv D E F A 10 5 0 B 0 0 0 C 0 0 0, , x = H, y = J, z = M wv D E F A 0 1 0 B 0 0 0 C 0 0 0, , x = I, y = J, z = M wv D E F A 0 0 0 B 0 0 0 C 0 0 0, , x = G, y = K, z = M wv D E F A 0 0 0 B 0 0 0 C 0 0 0, , x = H, y = K, z = M wv D E F A 0 4 0 B 0 0 5 C 0 0 0, , x = I, y = K, z = M wv D E F A 0 0 0 B 0 0 0 C 0 0 0, , x = G, y = L, z = M wv D E F A 0 0 0 B 0 0 0 C 0 0 0, , x = H, y = L, z = M wv D E F A 0 0 0 B 0 0 0 C 0 0 0, , x = I, y = L, z = M wv D E F A 0 0 0 B 0 0 0 C 0 0 0, , x = G, y = J, z = N wv D E F A 0 0 0 B 0 0 0 C 0 0 0, , x = H, y = J, z = N wv D E F A 0 0 0 B 0 0 0 C 0 0 0, , x = I, y = J, z = N wv D E F A 0 0 0 B 0 0 0 C 0 0 0, , x = G, y = K, z = N wv D E F A 0 0 0 B 0 0 0 C 0 0 0, , x = H, y = K, z = N wv D E F A 0 0 0 B 0 0 7 C 0 0 0, , x = I, y = K, z = N wv D E F A 0 0 0 B 0 0 0 C 0 0 0, , x = G, y = L, z = N wv D E F A 0 0 0 B 0 0 0 C 0 0 0, , x = H, y = L, z = N wv D E F A 0 0 0 B 0 0 3 C 0 0 0, , x = I, y = L, z = N wv D E F A 0 0 0 B 0 0 1 C 0 0 4, , x = G, y = J, z = O wv D E F A 0 0 0 B 0 0 0 C 0 0 0, , x = H, y = J, z = O wv D E F A 0 0 0 B 0 0 0 C 0 0 0, , x = I, y = J, z = O wv D E F A 0 0 0 B 0 0 0 C 0 0 0, , x = G, y = K, z = O wv D E F A 0 0 0 B 0 0 0 C 0 0 0, , x = H, y = K, z = O wv D E F A 0 0 0 B 0 0 0 C 0 0 0, , x = I, y = K, z = O wv D E F A 0 0 0 B 0 0 0 C 0 0 0, , x = G, y = L, z = O wv D E F A 0 0 0 B 0 0 0 C 0 0 0, , x = H, y = L, z = O wv D E F A 0 0 0 B 0 0 0 C 0 0 0, , x = I, y = L, z = O wv D E F A 0 0 0 B 0 0 0 C 0 0 10


Like Article

Suggest improvement

Next

How to Calculate Deciles in R?

Share your thoughts in the comments

Please Login to comment...

How to Use xtabs() in R to Calculate Frequencies? - GeeksforGeeks (2024)
Top Articles
Latest Posts
Article information

Author: Lidia Grady

Last Updated:

Views: 5828

Rating: 4.4 / 5 (45 voted)

Reviews: 84% of readers found this page helpful

Author information

Name: Lidia Grady

Birthday: 1992-01-22

Address: Suite 493 356 Dale Fall, New Wanda, RI 52485

Phone: +29914464387516

Job: Customer Engineer

Hobby: Cryptography, Writing, Dowsing, Stand-up comedy, Calligraphy, Web surfing, Ghost hunting

Introduction: My name is Lidia Grady, I am a thankful, fine, glamorous, lucky, lively, pleasant, shiny person who loves writing and wants to share my knowledge and understanding with you.