How to use xtabs() function to calculate one way frequency In R Language
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
How to Use xtabs() in R to Calculate Frequencies?
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.