Working with CSV files in R Programming
CSV (Comma-Separated Values) files are plain text files where each row contains data values separated by commas or other delimiters such as tabs. These files are commonly used for storing tabular data and can be easily imported and manipulated in R. We will explore how to efficiently work with CSV files in R Programming Language. We will cover key functions for reading, querying, and writing CSV data, along with practical examples and explanations.
Getting and Setting the Working Directory
Before working with CSV files, it is important to know and set the working directory where your CSV files are stored.
print(getwd())
setwd("/Example_Path/")
print(getwd())
getwd()
retrieves the current working directory in R.setwd()
changes the working directory to the specified path.
Example Output:
"C:/Users/GFG19565/Documents"
[1]"C:/Users/GFG19565/Documents"
1. Sample CSV File Example
Consider the following sample CSV data saved as sample.csv
:
id,name,department,salary,projects
1,A,IT,60754,4
2,B,Tech,59640,2
3,C,Marketing,69040,8
4,D,Marketing,65043,5
5,E,Tech,59943,2
6,F,IT,65000,5
7,G,HR,69000,7
We can create this file using any text editor (like notepad) and save it to your working directory.
2. Reading CSV Files into R
We can load a CSV file into R as a data frame using the read.csv()
function. The ncol()
and nrow()
return the number of columns and rows in the data frame, respectively.
csv_data <- read.csv(file = 'C:\\Users\\GFG19565\\Downloads\\sample.csv')
return(csv_data)
print(ncol(csv_data))
print(nrow(csv_data))
Output:

3. Querying Data in CSV Files
We can perform queries on your CSV data using base R functions to filter and summarize data.
1. Find minimum projects
Uses the min()
function on the projects
column to find the smallest value.
min_pro <- min(csv_data$projects)
print(min_pro)
Output:
[1] 2
2. Filter employees with salary greater than 60000 and select columns
Here we filter rows where salary
exceeds 60000 and selects only name
and salary
columns from filtered data.
result <- csv_data[csv_data$salary > 60000, c("name", "salary")]
print(result)
Output:

Writing Data to CSV Files
We can write processed data back into a CSV file using write.csv()
.
1. Calculate average salary per department
The tapply() function
applies the mean()
function to salary
grouped by department
.
result <- tapply(csv_data$salary, csv_data$department, mean)
result_df <- data.frame(Department = names(result), AverageSalary = as.vector(result))
write.csv(result_df, "Mean_salary.csv", row.names = FALSE)
Output:

2. Calculate total number of projects handled per department and write to CSV
The tapply() function is used
to compute the total number of projects handled in each department. The result is converted into a data frame for better structure and then written to a CSV file named department_project_totals.csv
.
total_projects <- tapply(csv_data$projects, csv_data$department, sum)
projects_df <- data.frame(Department = names(total_projects), TotalProjects = total_projects)
write.csv(projects_df, "department_project_totals.csv", row.names = FALSE)
Output:

In this article, we explored how to work with CSV files in R, including reading, querying, and writing CSV data.