Home » R CSV File

R CSV Files

A Comma-Separated Values (CSV) file is a plain text file which contains a list of data. These files are often used for the exchange of data between different applications. For example, databases and contact managers mostly support CSV files.

These files can sometimes be called character-separated values or comma-delimited files. They often use the comma character to separate data, but sometimes use other characters such as semicolons. The idea is that we can export the complex data from one application to a CSV file, and then importing the data in that CSV file to another application.

Storing data in excel spreadsheets is the most common way for data storing, which is used by the data scientists. There are lots of packages in R designed for accessing data from the excel spreadsheet. Users often find it easier to save their spreadsheets in comma-separated value files and then use R’s built-in functionality to read and manipulate the data.

R allows us to read data from files which are stored outside the R environment. Let’s start understanding how we can read and write data into CSV files. The file should be present in the current working directory so that R can read it. We can also set our directory and read file from there.

R CSV Files

Getting and setting the working directory

In R, getwd() and setwd() are the two useful functions. The getwd() function is used to check on which directory the R workspace is pointing. And the setwd() function is used to set a new working directory to read and write files from that directory.

Let’s see an example to understand how getwd() and setwd() functions are used.

Example

Output

R CSV Files

Creating a CSV File

A text file in which a comma separates the value in a column is known as a CSV file. Let’s start by creating a CSV file with the help of the data, which is mentioned below by saving with .csv extension using the save As All files(*.*) option in the notepad.

Example: record.csv

Output

R CSV Files

Reading a CSV file

R has a rich set of functions. R provides read.csv() function, which allows us to read a CSV file available in our current working directory. This function takes the file name as an input and returns all the records present on it.

Let’s use our record.csv file to read records from it using read.csv() function.

Example

When we execute above code, it will give the following output

Output

R CSV Files

Analyzing the CSV File

When we read data from the .csv file using read.csv() function, by default, it gives the output as a data frame. Before analyzing data, let’s start checking the form of our output with the help of is.data.frame() function. After that, we will check the number of rows and number of columns with the help of nrow() and ncol() function.

Example

When we run above code, it will generate the following output:

Output

R CSV Files

From the above output, it is clear that our data is read in the form of the data frame. So we can apply all the functions of the data frame, which we have discussed in the earlier sections.

R CSV Files

Example: Getting the maximum salary

Output

R CSV Files

Example: Getting the details of the person who have a maximum salary

Output

R CSV Files

Example: Getting the details of all the persons who are working in the IT department

Output

R CSV Files

Example: Getting the details of the persons whose salary is greater than 600 and working in the IT department.

Output

R CSV Files

Example: Getting details of those peoples who joined on or after 2014.

Output

R CSV Files

Writing into a CSV file

Like reading and analyzing, R also allows us to write into the .csv file. For this purpose, R provides a write.csv() function. This function creates a CSV file from an existing data frame. This function creates the file in the current working directory.

Let’s see an example to understand how write.csv() function is used to create an output CSV file.

Example

Output

R CSV Files


Next TopicR Excel file

You may also like