Home » R16 May 2017

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):

Read CSV file

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

Get Number of Rows and Columns in CSV

Get maximum marks in the CSV

Get the student with the highest mark

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

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

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.

Hope this helps. Thanks.

R

Get New Post by Email

Find me on

FacebookTwitterGoogle+LinkedInRSS Feed