R: Read Write CSV

This article shows how to read and write CSV file with R programming language.

We will be manually creating a new CSV file with the following data:

rollno,name,gender,age,marks,address
7,Ram,M,22,77,Kathmandu
9,Sita,F,23,88,Bhaktapur
14,Hari,M,24,74,Lalitpur
22,Radha,F,25,65,Bhaktapur
3,John,M,23,90,Kathmandu
11,Jia,F,21,55,Kathmandu

We name the CSV file as students.csv. The CSV file contains data of students. It contains roll no, name, gender, age, marks obtained in a subject, and address of the students.

Go to the directory where you created the above CSV file (i.e. setting working directory):


> print (getwd()) # get current working directory
[1] "/home/mukesh"

> setwd('/home/mukesh/Documents/training') # set working directory 
> print (getwd())
[1] "/home/mukesh/Documents/training"

Read CSV file

Read and print the data of the students.csv file that we just created above.


> data <- read.csv('students.csv') # read CSV file
> print (data)
  rollno  name gender age marks   address
1      7   Ram      M  22    77 Kathmandu
2      9  Sita      F  23    88 Bhaktapur
3     14  Hari      M  24    74  Lalitpur
4     22 Radha      F  25    65 Bhaktapur
5      3  John      M  23    90 Kathmandu
6     11   Jia      F  21    55 Kathmandu

Get Number of Rows and Columns in CSV


> print (ncol(data)) # number of columns in the csv data
[1] 6

> print (nrow(data)) # number of rows in the csv data
[1] 6

Get maximum marks in the CSV


> maxMarks <- max(data$marks) # maximum marks in the data
> print (maxMarks)
[1] 90

Get the student with the highest mark


> topperData <- subset(data, marks == max(marks)) # row of the person getting max marks
> print (topperData)
  rollno name gender age marks   address
5      3 John      M  23    90 Kathmandu

Get data of students from a particular address only, e.g. Kathmandu


> kathmanduData <- subset(data, address == 'Kathmandu') # students from kathmandu only
> print (kathmanduData)
  rollno name gender age marks   address
1      7  Ram      M  22    77 Kathmandu
5      3 John      M  23    90 Kathmandu
6     11  Jia      F  21    55 Kathmandu

Get data of students from a particular address and with a particular gender, e.g. Kathmandu & Male


> kathmanduMale <- subset(data, address == 'Kathmandu' & gender == 'M') # male students from kathmandu
> print (kathmanduMale)
  rollno name gender age marks   address
1      7  Ram      M  22    77 Kathmandu
5      3 John      M  23    90 Kathmandu

Write Data to CSV

In below example, we read data from students.csv file.
Then, we filter the data by address. We only select those students whose address is Kathmandu.
After that, we write the filtered result data into a new CSV file named students-kathmandu.csv.


> data <- read.csv('students.csv') # read CSV file
> kathmanduData <- subset(data, address == 'Kathmandu') # students from kathmandu only
> write.csv(kathmanduData, "students-kathmandu.csv") # write specified data to new csv file
> newData <- read.csv('students-kathmandu.csv') # read csv file
> print (newData)
  X rollno name gender age marks   address
1 1      7  Ram      M  22    77 Kathmandu
2 5      3 John      M  23    90 Kathmandu
3 6     11  Jia      F  21    55 Kathmandu

Hope this helps. Thanks.