Create table from DataFrame in R
In this article, we are going to discuss how to create a table from the given Data-Frame in the R Programming language.
Function Used:
table(): This function is an essential function for performing interactive data analyses. As it simply creates tabular results of categorical variables.
Syntax: table(…, exclude = if (useNA == “no”) c(NA, NaN),useNA = c(“no”, “ifany”, “always”), dnn = list.names(…), deparse.level = 1)
Returns: It will return the frequency tables with conditions and cross-tabulations.
Example 1: Creating a frequency table of the given data frame in R language:-
In this example, we will be building up the simple frequency table in R language using the table() function in R language. This table just providing the frequencies of elements in the dataframe.
R
gfg_data <- data.frame (
Country = c ( "France" , "Spain" , "Germany" , "Spain" , "Germany" ,
"France" , "Spain" , "France" , "Germany" , "France" ),
age = c (44,27,30,38,40,35,52,48,45,37),
salary = c (6000,5000,7000,4000,8000),
Purchased= c ( "No" , "Yes" , "No" , "No" , "Yes" ,
"Yes" , "No" , "Yes" , "No" , "Yes" ))
gfg_table<- table (gfg_data$Country)
gfg_table
|
Output:
France Germany Spain
4 3 3
Example 2: Creating a frequency table with the proportion of the given data frame in R language:
Here, we will be using the prop.table() function which works quite similar to the simple table() function to get the frequency table with proportion from the given data frame.
R
gfg_data <- data.frame (
Country = c ( "France" , "Spain" , "Germany" , "Spain" , "Germany" ,
"France" , "Spain" , "France" , "Germany" , "France" ),
age = c (44,27,30,38,40,35,52,48,45,37),
salary = c (6000,5000,7000,4000,8000),
Purchased= c ( "No" , "Yes" , "No" , "No" , "Yes" , "Yes" ,
"No" , "Yes" , "No" , "Yes" ))
gfg_table = as.table ( table (gfg_data$Country))
prop.table (gfg_table)
|
Output:
France Germany Spain
0.4 0.3 0.3
Example 3: Creating a frequency table with condition from the given data frame in R language:
In this example, we will be building up the simple frequency table in R language using the table() function with a condition inside it as the function parameter R language. This table just providing the frequencies of elements that match the given conditions in the function in the data frame.
Here we will be making a frequency table of the salary column with the condition of a salary greater than 6000 from the data frame using the table() function in R language.
R
gfg_data <- data.frame (
Country = c ( "France" , "Spain" , "Germany" , "Spain" , "Germany" ,
"France" , "Spain" , "France" , "Germany" , "France" ),
age = c (44,27,30,38,40,35,52,48,45,37),
salary = c (6000,5000,7000,4000,8000),
Purchased= c ( "No" , "Yes" , "No" , "No" , "Yes" , "Yes" ,
"No" , "Yes" , "No" , "Yes" ))
gfg_table = table (gfg_data$salary>6000)
gfg_table
|
Output:
FALSE TRUE
6 4
Example 4: Creating a 2–way cross table from the given data frame in R language:
In this example, we will be building up the simple 2-way cross table in R language using the table() function R language. This table just providing the frequencies of elements of the different columns in the data frame.
R
gfg_data <- data.frame (
Country = c ( "France" , "Spain" , "Germany" , "Spain" , "Germany" ,
"France" , "Spain" , "France" , "Germany" , "France" ),
age = c (44,27,30,38,40,35,52,48,45,37),
salary = c (6000,5000,7000,4000,8000),
Purchased= c ( "No" , "Yes" , "No" , "No" , "Yes" , "Yes" ,
"No" , "Yes" , "No" , "Yes" ))
gfg_table = table (gfg_data$salary,gfg_data$Country)
gfg_table
|
Output:
France Germany Spain
4000 0 1 1
5000 0 0 2
6000 2 0 0
7000 1 1 0
8000 1 1 0
Last Updated :
07 Apr, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...