We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 9
‘824, 4-47 PM Reading the CSV fle nto Dataframes in R - GeoksforGueks
OG
Data Visualization StatisticsinR Machine LearninginR DataScienceinR PackagesinR Data Types Str
Reading the CSV file into Dataframes in R
Last Updated : 09 May, 2021
In this article, we will learn how to import or read a CSV file into a
dataframe in R Programming Language.
Data set in use:
SampleData
luser_id age gender student employed cye freq distance time speed
4) 23)M 1 Dally 225] 1819
baiy aa] 6) 1099
a 0 Daly 550/28 1458
4| alr 0 1 Less than once amonth 3.24/24 8.1
5 aM 01 Shoalimesperaesk 7.81/26, 1802
6 30/M ° 1 Several times per week 3) 9
745M 0 1 Sevealtinesperweck 19.03) 51 1838
8 eM 0 1 Severltinesperweek | 6.21] 30/988
9 39/M o 1 Severaliimes per week 19.95) 50 16.74
40, 44) ° 0 Severaltimes permonth 7.4, 4410.09
Step 1: Set or change the working directory
In order to import or read the given CSV file into our data frame, we first
need to check our current working directory, and make sure that the CSV file
is in the same directory as our R studio is in, or else it might show “File not
found Error”.
To check the current working directory we need to use getwd() function, and
to change the current working directory to some other working directory, we
need to use stewd/() function
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, Got it!
Policy
-nips:teww.geekstorgecks.orgreading-the-csv-le-into-datarames-ir-! 119‘824, 4-47 PM Reading the CSV fle nto Dataframes in R - GeoksforGueks
getwd() returns an absolute file-path representing the current working
directory of the R process.
Syntax:
getwa()
setwd(dir) used to set the working directory to dir.
Syntax:
setwd(path)
Example:
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
-nips:teww.geekstorgecks.orgreading-the-csv-le-into-datarames-ir-!‘824, 4-47 PM Reading the CSV fle nto Dataframes in R - GeoksforGueks
R
# gives the current working directory
getwd()
# changes the location
setwd("C: /Users/Vanshi/Desktop/gfg")
Output:
C:/Users/Vanshi/Documents
Step 2: Read the CSV file
Now that we have set our working path, we will import the CSV file into the
data frame, and name our data frame as sdata.
Here, we are reading the .csv file named “SampleData” using read.csv
command, into our R studio, which means we are feeding the values to the
Rstudio to extract some important information out of it.
read.csv() function reads a file in table format and creates a data frame from
it, with cases corresponding to lines and variables to fields in the file.
dec
Syntax: read.csv(file, header
= TRUE, comment.char = “",..)
TRUE, sep = ",", quote
Arauments:
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
-nips:teww.geekstorgecks.orgreading-the-csv-le-into-datarames-ir-!‘824, 4-47 PM
Reading the CSV fle ito Datarames in GooksorGooks
header: a logical value indicating whether the file contains the
names of the variables as its first line. If missing, the value is
determined from the file format: header is set to TRUE if and only if
the first row contains one fewer field than the number of columns.
sep: the field separator character. Values on each line of the file are
“" (the default for read.table)
the separator is ‘white space’ that is one or more spaces, tabs,
separated by this character. If sep
newlines or carriage returns.
quote: the set of quoting characters.
dec: the character used in the file for decimal points.
fill: logical. If TRUE then in case the rows have unequal length,
blank fields are implicitly added.
comment.char: character: a character vector of length one
containing a single character or an empty string.
Further arguments to be passed.
Example:
R
sdata <- read.csv("Samplebata.csv", header = TRUE, sep =
sdata
# views the data frame formed from the csv file
View(sdata)
Output:
[userid [age *[gender *[student *|employed °| eye freq distance ‘|time "| speed
1 1M 1 1 Daily 2251S 13.00
2 2 3sM ° 1 Daly Lu sae
3 3s ° 1 aly 559, 23458
4 ae ° 1 Less than ance a month ae 80
5 san ° 1) Several times per week zal, 26 soz
6 636M ° 1 Several times er week 300, 20,900
7 74s @ 1 | evra cles per week Bests
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
-nips:teww.geekstorgecks.orgreading-the-csv-le-into-datarames-ir-!‘124,447 PM Reading the CSV fle ito Datarames in GooksorGooks
Now that, we have created our dataframe, we can perform some operations
onit. The data read according to the usage from dataframe. Given below are
two examples who read the data as per their requirement.
Example 1:
R
sdata <- read.csv(
“samplebata.csv", header = TRUE, sep
highspeed <- subset(
sdata, sdata$speed == nax(sdatasspeed))
# views the subsetted value in
# tabular form
View(highspeed)
Output:
“userid age gender student employed eye freq distance time speed
ss aim ° 1 overal times per waek 7.81 26 802
Example 2:
R
sdata <- read.csv(
"SampleData.csv", header = TRUE, sep = ",")
highfreq <- subset(
sdata, sdata$cyc_freq
‘Several times per week")
#t views the information, of the above
# condition in tabular format
View(highfreq)
Output:
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
-nips:teww.geekstorgecks.orgreading-the-csv-le-into-datarames-ir-!‘824, 4-47 PM Reading the CSV fle nto Dataframes in R - GeoksforGuoks
~ user age gender student employed eye freq distance tume "speed
5 5 aM ° 1, Several times per week 781-26 1802
6 6) 36M o 1. Several times per week 300 209.00
7 7m o 1. Several times per week 1503S 15.33,
8 5 sam ° 1, Several tes per week 62138,
9 9) 39M o 1 Several times per week 195 S06 74
Learn in a distraction-free environment with refined, high-quality content
and 35+ expert-led tech courses to help you crack any interview. From
programming languages and DSA to web development and data science,
GeeksforGeeks Premium has you covered!
Choose GeeksforGeeks Premium today and also get access to Unlimited
Article Summarization, 100% Ad free environment, A.|. Bot support in all
coding problems, and much more. Go Premium!
S. shilpi... a 2
Next Article
R Read Text File to DataFrame
Similar Reads
Read contents of a CSV File in R Programming - read.csv() Function
read.csv() function in R Language is used to read "comma separated value”
files. It imports data in the form of a data frame. Syntax: read.csv(file, header...
3min read
Reading large numeric TSV file into memory in R
TSV (Tab-Separated Values) files are a common way of storing data. When
working with large numeric TSV files in the R programming language, memor.
4 min read
How ta Imnart a CSV File inte R?
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
-nips:tteww.geekstorgecks.orgreading-the-csv-fle-into-datarames-ir-!‘828, 4-47 PM Reading the CSV fle nto Dataframes in R - GeoksforGueks
Reading contents of a Text File in R Programming - read.table() Function
The read.table() function in R can be used to read a text file's contents. A
versatile and often used function for reading tabular data from different file...
3min read
Specify Row Names when Reading Excel File in R
In this article, we are going to specify the row names when reading Excel file in
the R Programming language. Specifying row names when reading a file usin.
2min read
How to convert CSV into array in R?
In this article, we are going to see how to convert CSV into an array in R
Programming Language. If we load this file into any environment like python,
2min read
How to Read a CSV from URL into R?
In this article, we are going to see how to read CSV files from URL using R
Programming Language. Method 1: Using Base R Here we are using read.csv..
Lmin read
How to create matrix and vector from CSV file in R?
In this article, we will discuss how to convert CSV data into a matrix and a
vector in R Programming Language. We will use read.csv() function to load th...
2min read
Read CSV file and select specific rows and columns in R
In this article, we are going to see how to read CSV file and select specific
rows and columns in R Programming Language. CSV file: To import a CSV fil.
Lmin read
How to plot a graph in R using CSV file?
To plot a graph in R using a CSV file, we need a CSV file with two-column, the
values in the first column will be considered as the points at the x-axis and th
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
itps:wwn.geeksforgeeks.orgireading-tne-osvfle-into-datatrames-imt! 718‘824, 4-47 PM Reading the CSV fle nto Dataframes in R - GeoksforGueks
Article Tags : Ranguage CSV
OG GeeksforGeeks
Corporate & Communications Address:-
4-143, 9th Floor, Sovereign Corporate
Tower, Sector- 136, Noida, Uttar Pradesh
(201305) | Registered Address:-K 061,
Tower k, Gulshan Vivante Apartment,
Sector 137, Noida, Gautam Buddh
Nager, Uttar Pradesh, 201305
scr
Company Languages
About Us Python
Legal Java
In Media ce
Contact Us Pe
Advertise with us Gotang
‘SFG Corporate Solution sau
Placement Training Program Language
GeeksforGeeks Community [Android Tutorial
Tutorials Archive
DSA
Data Structures
Data Science & ML
Data Science With Python
Algorithms Data Science For Beginner
Machine Learning
ML Maths
DSA for Beginners
Basic DSA Problems
DSA Roadmap Data Visuaisat
Top 100 DSAInterview Problems
Pandas
DSA Roadmap by Sandeep Jain NumPy
All Cheat Sheets NUP
Deep Learning
Web Technologies
Python Tutorial
HTML Python Programming Examples
css Python Projects
Javascript
Python Tkinter
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
-nips:teww.geekstorgecks.orgreading-the-csv-le-into-datarames-ir-!Reading the CSV fle nto Dataframes in R- GeokstorGeoks
‘824, 4-47 PM
Web Design
Computer Science
Operating Systems
‘Computer Network
Database Management System
Software Engineering
Digital Logic Design
Engineering Maths
Software Development
Software Testing
‘System Design
High Level Design
Low Level Design
UML Diagrams.
Interview Guide
Design Patterns
ono
system Design Bootcamp
Interview Questions
School Subjects
Mathematics
Physies
Chemistry
Biology
Social Science
English Grammar
Commerce
World 6k
\GeeksforGeeks, Sanchhaya Educatio!
DevOps
sit
Linux
ws
Docker
kKubernetes
oure
eee
Devops Roadmap
Inteview Preparation
Competitive Programming
‘op 0S or Algo for cP
Company-Wise Recruitment Process
Company-Wise Preparation
Aptitude Preparation
Puzzles
GeeksforGeeks Videos
DSA
Python
Java
cH
Web Development
Data Science
cS Subjects
Private Limited, Al
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,
-nips:teww.geekstorgecks.orgreading-the-csv-le-into-datarames-ir-!
Policy