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 expand dataframe in r
Next article icon

How to create dataframe in R

Last Updated : 18 Apr, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

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 and manipulation. In this article, we'll explore different methods to create data frames in R.

Create a data frame Using the data.frame() Function

The most common method to create a data frame in R is by using the data.frame() function. This function takes vectors as input and combines them into a data frame.

R
# Creating vectors name <- c("Johny", "Ali", "Boby", "Emilya") age <- c(25, 30, 22, 28) salary <- c(50000, 60000, 45000, 55000) # Creating a dataframe df <- data.frame(Name = name, Age = age, Salary = salary) # Displaying the dataframe print(df) 

Output:

    Name Age Salary 1  Johny  25  50000 2    Ali  30  60000 3   Boby  22  45000 4 Emilya  28  55000

In this example, we create vectors for name, age, and salary, and then use the data.frame() function to combine them into a dataframe called df.

Create a data frame Using the read.table() or read.csv() Functions

Another way to create a dataframe in R is by reading data from an external file using functions like read.table() or read.csv(). These functions read data from text files (such as CSV files) and automatically convert them into dataframes. Here's an example using read.csv():

Read data from a tab-separated file

df <- read.table("data.tsv", sep = "\t", header = TRUE)

data.csv is a CSV file containing tabular data. The read.csv() function reads this file and creates a dataframe df.

Create a data frame Using the tibble Package

The tibble package provides an alternative to the base R dataframe with some additional features. You can create a tibble using the tibble() function. Here's an example:

R
# Installing and loading the tibble package install.packages("tibble") library(tibble) # Creating a tibble df <- tibble(   Name = c("Johny", "Ali", "Boby", "Emilya"),   Age = c(25, 30, 22, 28),   Salary = c(50000, 60000, 45000, 55000) ) # Displaying the tibble print(df) 

Output:

# A tibble: 4 × 3   Name     Age Salary   <chr>  <dbl>  <dbl> 1 Johny     25  50000 2 Ali       30  60000 3 Boby      22  45000 4 Emilya    28  55000

In this example, we use the tibble() function from the tibble package to create a tibble named df.

Conclusion

Creating dataframes is a fundamental task in R for data analysis and manipulation. In this article, we discussed three methods to create dataframes: using the data.frame() function, reading data from external files using read.table() or read.csv() functions, and creating tibbles using the tibble package. Depending on your data and requirements, you can choose the method that best suits your needs.


Next Article
how to expand dataframe in r

M

manojjai07nu
Improve
Article Tags :
  • R Language
  • R DataFrame-Function

Similar Reads

  • 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: tabl
    3 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, 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
  • how to expand dataframe in r
    In this article, we will discuss how to expand the data frame in R Programming Language. Now create a Data Frame of all the Combinations of Vectors passed as Arguments in R Programming using expand.grid() Function. expand.grid() Functionexpand.grid() function in R Programming Language is used to cre
    2 min read
  • How to merge dataframes in R ?
    In this article, we will discuss how to perform inner, outer, left, or right joins in a given dataframe in R Programming Language. Functions Used merge() function is used to merge or join two tables. With appropriate values provided to specific parameters, we can create the desired join. Syntax: mer
    3 min read
  • How to Delete DataFrames in R?
    In R, a DataFrame is a data structure which can be two-dimensional, that is it can be used to hold data in rows and columns. To create a DataFrame, you can use the data.frame() function. but after you're done with a DataFrame, you may wish to remove it so that memory can be released or your workspac
    3 min read
  • How to Fix Error in aggregate.data.frame in R
    The aggregate function in R applies the data aggregation on the basis of required factors. Yet, users are bound to find errors while dealing with data frames. In this article, common errors and effective solutions to solve them are elucidated. Common Errors in aggregate.data.frameErrors may arise, p
    2 min read
  • How to Export DataFrame to CSV in R ?
    R Programming language allows us to read and write data into various files like CSV, Excel, XML, etc. In this article, we are going to discuss how to Export DataFrame to CSV file in R Programming Language. Approach:  Write Data in column wise formatCreate DataFrame for these dataWrite Data to the CS
    1 min read
  • How to Convert a List to a Dataframe in R
    We have a list of values and if we want to Convert a List to a Dataframe within it, we can use a as.data.frame. it Convert a List to a Dataframe for each value. A DataFrame is a two-dimensional tabular data structure that can store different types of data. Various functions and packages, such as dat
    4 min read
  • How to Address Error in as.data.frame in R
    The as.data.frame() function is frequently used to convert different types of objects, such as matrices, lists, or factors, into data frames. However, users may encounter errors during this conversion process. this article explains common errors with as.data.frame() and how to resolve them. Common E
    2 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