R CSV Files

R provides functions to read and write to various file formats. In this R Tutorial, we shall look specifically into CSV Files.

We shall learn R functions to :

Example of a CSV File, that we use in the examples going forward, is given below :

</>
Copy
Andrew,28,25.2
Mathew,23,10.5
Dany,49,11
Philip,29,21.9
John,38,44
Bing,23,11.5
Monica,29,45

You may refer R Working Directory to modify the path of R Workspace to point to the directory containing your input files (CSV Files).

Read CSV Files

CSV Files are those files with values separated by commas in each row. Each row corresponds to a record or observation.

The syntax of function to read CSV File in R programming language is

</>
Copy
read.csv(<filename>)

Example 1 – Read CSV File in R

In this example, we will read a CSV File using read.csv() function.

r_readCSVexample.R

</>
Copy
# Example R program to read CSV File
#set working directory - the directory containing csv file
setwd("/home/arjun/workspace/r")
#read csv file
csvData = read.csv("sampleCSV.csv")
# print the data type of csvData 
cat("CSV Data type : ",class(csvData), "\n\n")
print(csvData)

Output

$ Rscript r_readCSVexample.R 
CSV Data type :  data.frame 
    name age income
1 Andrew  28   25.2
2 Adarsh  23   10.5
3   Dany  49   11.0
4 Philip  29   21.9
5   John  38   44.0
6   Bing  23   11.5
7 Monica  29   45.0

Please observe that the data of csv file is read to an R Data Frame.

Process data read from CSV Files

R programming language reads the CSV File to an R Data frame. So, you may use all the R Data Frame functions to process the data.

Example 2 – Process CSV Data in R

In this example, we will read a CSV File and then process this data. We will extract rows, whose income is equal to the maximum of income.

r_csv_analyze_data.R

</>
Copy
# Example R program to analyze CSV File
#set working directory - the directory containing csv file
setwd("/home/arjun/workspace/r")
#read csv file
celebrities = read.csv("sampleCSV.csv")
# retrieve rows based on a condition
maxSalariedCelebrities = subset(celebrities, income==max(income))
# print the result
print(maxSalariedCelebrities)

Output

$ Rscript r_csv_analyze_data.R 
    name age income
7 Monica  29     45

Write transformed data to CSV Files

Once we extract the required data or transform the data which is in data frame, we may write data frame to a CSV File.

Example 3 – Write Transformed Data to CSV in R

In this example, we will extract the rows with maximum income, and write these rows to a CSV File.

r_csv_write_data.R

</>
Copy
# Example R program to write data to a CSV file
#set working directory - the directory containing csv file
setwd("/home/arjun/workspace/r")
#read csv file
celebrities = read.csv("sampleCSV.csv")
# retrieve rows based on a condition
maxSalariedCelebrities = subset(celebrities, income==max(income))
# write filtered data into a new CSV file.
write.csv(maxSalariedCelebrities,"result.csv")

Output

$ Rscript r_csv_write_data.R 
R CSV Files - Read CSV File using R - Write to CSV File in R

Conclusion

In this R Tutorial – R CSV Files, we have learnt to read CSV File using R, process data read from CSV using R and write process data to CSV using R.