// Tutorial //

Reading the CSV file into Data frames in R

Published on August 3, 2022
Default avatar
By Prajwal CN
Developer and author at DigitalOcean.
Reading the CSV file into Data frames in R

While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the “report an issue“ button at the bottom of the tutorial.

With the help of specific functions offered by R, reading the CSV files into data frames is much easier.

What is a CSV file?

CSV is expanded as Comma, Separated, Values. In this file, the values stored are separated by a comma. This process of storing the data is much easier.

Why CSV is the most used file format for data storing?

Storing the data in an excel sheet is the most common practice in many companies. In the majority of firms, people are storing data as comma-separated-values (CSV), as the process is easier than creating normal spreadsheets. Later they can use R’s built in packages to read and analyze the data.

Being the most popular and powerful statistical analysis programming language, R offers specific functions to read data into organized data frames from a CSV file.

Reading CSV File to Data Frame

In this short example, we will see how we can read a CSV file into organized data frames.

The first thing in this process is to getting and setting up the working directory. You need to choose the working path of the CSV file.

1. Setting up the working directory

Here you can check the default working directory using getwd() function and you can also change the directory using the function setwd().

>getwd() #Shows the default working directory 

---->   "C:/Users/Dell/Documents"

> setwd("C:\Users\Dell\Documents\R-test data") #to set the new working Directory

> getwd() #you can see the updated working directory

---> "C:/Users/Dell/Documents/R-test data"

2. Importing and Reading the dataset / CSV file

After the setting of the working path, you need to import the data set or a CSV file as shown below.

> readfile <- read.csv("testdata.txt")

Execute the above line of code in R studio to get the data frame as shown below.

loading csv files in R

To check the class of the variable ‘readfile’, execute the below code.

> class(readfile)

---> "data.frame"            

In the above image you can see the data frame which includes the information of student names, their ID’s, departments, gender and marks.

3. Extracting the student’s information from the CSV file

After getting the data frame, you can now analyse the data. You can extract particular information from the data frame.

To extract the highest marks scored by students,

>marks <- max(data$Marks.Scored) #this will give you the highest marks

#To extract the details of a student who scored the highest marks,

> data <- read.csv("traindata.csv")

> Marks <- max(data$Marks.Scored)

> retval <- subset(data, Marks.Scored == max(Marks.Scored))   #This will
 extract the details of the student who secured highest marks 

> View(retval)

reading csv files into dataframe in R

To extract the details of the students who are in studying in ‘chemistry’ Dept,

> readfile <- read.csv("traindata.csv")

> retval <- subset( data, Department == "chemistry")  # This will extract the student details who are in Biochemistry department 
> View(retval)

extracting student information from csv files in R


By this process you can read the csv files in R with the use of read.csv(“ “) function. This tutorial covers how to import the csv file and reading the csv file and extracting some specific information from the data frame.

I used R studio for this project. RStudio offers great features like console, editor, and environment as well. Anyhow you are free to use other editors like Thinn-R, Crimson editor, etc. I hope this tutorial will help you in understanding the reading of CSV files in R and extracting some information from the data frame.

For more read: https://cran.r-project.org/manuals.html

If you’ve enjoyed this tutorial and our broader community, consider checking out our DigitalOcean products which can also help you achieve your development goals.

Learn more here

About the authors
Default avatar
Developer and author at DigitalOcean.

Still looking for an answer?

Was this helpful?

Thank you for the blog

- Shriganesh Bhide