Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Data Visualization
  • Statistics in R
  • Machine Learning in R
  • Data Science in R
  • Packages in R
  • Data Types
  • String
  • Array
  • Vector
  • Lists
  • Matrices
  • Oops in R
Open In App
Next Article:
How to create dataframe in R
Next article icon

Create table from DataFrame in R

Last Updated : 07 Apr, 2021
Comments
Improve
Suggest changes
Like Article
Like
Report

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

Next Article
How to create dataframe in R
author
geetansh044
Improve
Article Tags :
  • R Language
  • R-DataFrame

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
  • Create data.frame from nested lapply's
    In R, nested `lapply()` functions can be used to create a data frame from a nested list. This approach allows you to apply a function to each element of the nested list and then convert the processed data into a structured tabular format. This can be useful when dealing with complex data structures
    3 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 Convert XML to DataFrame in R?
    A Data Frame is a two-dimensional and tabular data structure in the R that is similar to a table in the database or an Excel spreadsheet. It is one of the most commonly used data structures for the data analysis in R with the columns representing the various and rows representing the observations. X
    4 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
  • Convert DataFrame to vector in R
    In this article, we will discuss how a dataframe can be converted to a vector in R. For the Conversion of dataframe into a vector, we can simply pass the dataframe column name as [[index]]. Approach: We are taking a column in the dataframe and passing it into another variable by the selection method
    2 min read
  • How to Add Variables to a Data Frame in R
    In data analysis, it is often necessary to create new variables based on existing data. These new variables can provide additional insights, support further analysis, and improve the overall understanding of the dataset. R, a powerful tool for statistical computing and graphics, offers various metho
    5 min read
  • How to create, index and modify Data Frame in R?
    In this article, we will discuss how to create a Data frame, index, and modify the data frame in the R programming language. Creating a Data Frame:A Data Frame is a two-dimensional labeled data structure. It may consist of fields/columns of different types. It simply looks like a table in SQL or lik
    4 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences