Create table from DataFrame in R
Last Updated :
07 Apr, 2021
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
Similar Reads
How to create dataframe in R Dataframes are fundamental data structures in R for storing and manipulating data in tabular form. They allow you to organize data into rows and columns, similar to a spreadsheet or a database table. Creating a data frame in the R Programming Language is a simple yet essential task for data analysis
3 min read
Convert dataframe to data.table in R In this article, we will discuss how to convert dataframe to data.table in R Programming Language. data.table is an R package that provides an enhanced version of dataframe. Characteristics of data.table :Â data.table doesnât set or use row namesrow numbers are printed with a : for better readabilit
5 min read
Convert Tibble to Data Frame in R Tibbles are a type of data frame in R Programming Language that has an enhanced print method, making it easier to display data. However, in some situations, we may need to convert a tibble to a data frame. So, in this article, we will explore some approaches to convert tibbles to data frames. Tibble
4 min read
R - Create Dataframe From Existing Dataframe Create Dataframes when dealing with organized data so sometimes we also need to make Dataframes from already existing Dataframes. In this Article, let's explore various ways to create a data frame from an existing data frame in R Programming Language. Ways to Create Dataframe from Existing Dataframe
6 min read
How to Create Tables in R? In this article, we will discuss how to create tables in R Programming Language. Method 1: Create a table from scratch We can create a table by using as.table() function, first we create a table using matrix and then assign it to this method to get the table format. Syntax: as.table(data) Example: I
2 min read