Handling CSV files

Let's learn how to input and output data using .csv files

What are CSV files?

A CSV file is a Comma Separated Values file. It’s just like any other ordinary text file, however, it has the .csv extension and uses special characters as separators between data.

Suppose we have a table of students’ names, their ages, and their scores on a particular test. The table will look something like this.

Name Age Marks
Andrew 28 55.2
Mathew 23 99.5
Dany 29 71
Philip 29 21.9
John 28 44
Brian 23 11.5
Monica 29 45

In a .csv file format, we can store the above data as:

Name,   Age, Marks
Andrew, 28,  55.2
Mathew, 23,  99.5
Dany,   29,  71
Philip, 29,  21.9
John,   28,  44
Brian,  23,  11.5
Monica, 29,  45

CSV files can be used for exchanging data between different applications that is input and output data. CSV files mostly use the comma character to separate (or delimit) data, but sometimes use other characters, like semicolons ;.

Taking Input from CSV file

Taking input from a .csv file is easy. We use the function read.csv() to fetch all the data. Simply pass the file path to this function and you will get all the data.

Create a free account to access the full course.

By signing up, you agree to Educative's Terms of Service and Privacy Policy