How to Create and Write CSV file in R?

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

# Getting and printing current working directory.  
print(getwd())  
# Setting the current working directory.  
setwd("C:/Users/ajeet")  
# Getting and printingthe current working directory.  
print(getwd())

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

id,name,salary,start_date,dept  
1,Shubham,613.3,2012-01-01,IT  
2,Arpita,525.2,2013-09-23,Operations  
3,Vaishali,63,2014-11-15,IT  
4,Nishka,749,2014-05-11,HR  
5,Gunjan,863.25,2015-03-27,Finance  
6,Sumit,588,2013-05-21,IT  
7,Anisha,932.8,2013-07-30,Operations  
8,Akash,712.5,2014-06-17,Financ  

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

csv_data<- read.csv("record.csv")  
  
#Getting details of those peoples who joined on or after 2014  
details <- subset(csv_data,as.Date(start_date)>as.Date("2014-01-01"))  
  
# Writing filtered data into a new file.  
write.csv(details,"output.csv")  
new_details<- read.csv("output.csv")  
print(new_details)  

Output

R CSV Files