Open In App

How to edit CSV files in R

Last Updated : 27 Jan, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we are going to learn how to edit CSV files in the R programming language.

What is a CSV file?

A Comma Separated Values (CSV) file is a simple plain text file that contains a list of data separated by a delimiter. As the name implies in these files the information stored is separated by a comma. 

R has built-in functionality of CSV parser which is one of the most reliable and easiest ways to read, write, edit and process data from a CSV file.

Creating a CSV file

To create a CSV file we need to save data separated by commas in a text file and save that file with the .csv extension. Another way of creating a CSV file is using google sheets or excel. Let’s create a CSV file using the given below data and save it with the name shop.csv.

Product,Size,Color,Price
Shirt,Medium,Blue,1039
T-Shirt,small,Green,1899
Jeans,large,Black,1299
Shirt,Medium,White,1999

CSV File:

How to edit CSV files in R?

CSV File

Reading a CSV file

Note that the CSV file should be present in the current working directory otherwise we have to give the complete location address of that file. Now we are importing the CSV file that we created in R and printing the file and performing some operations such as extracting numbers of rows and columns using nrow() and ncol() methods and minimum and maximum values in a column using min() and max() methods in of a CSV file.

R




# Read shop.csv file
data <- read.csv("shop.csv")
# Print csv file
print(data)
# Printing total number of columns
cat("Total Columns: ", ncol(data))
# Print total number of rows
cat("Total Rows:", nrow(data))
# Store minimum value of Price column
min_price <- min(data$Price)
# Store maximum value of Price column
max_price <- max(data$Price)
cat("Min. price :",min_price)
cat("Max. price :",max_price)


Output :

  Product   Size Color Price
1   Shirt Medium  Blue  1039
2 T-Shirt  small Green  1899
3   Jeans  large Black  1299
4   Shirt Medium White  1999
Total Columns:  4
Total Rows: 4
Min. price : 1039
Max. price : 1999

Editing CSV File in R

Deleting and adding rows in a CSV file

To edit the CSV file what we do is delete a row from the CSV file that we have created in the previous step and after deleting the row append a new row using rbind() function by creating a new data frame for the row that has to be added in the CSV file.

R




# Read shop.csv file
data <- read.csv("shop.csv")
# Print csv file
print(data)
# Printing total number of columns
cat("Total Columns: ", ncol(data))
# Print total number of rows
cat("Total Rows:", nrow(data))
# Store minimum value of Price column
min_price <- min(data$Price)
# Store maximum value of Price column
max_price <- max(data$Price)
cat("Min. price :",min_price)
cat("Max. price :",max_price)
 
# Delete 4th row from data
data <- data[-c(4),]
# Assigning column values
Product <- c("Jacket")
Size <- c("Medium")
Color <- c("Cyan")
Price <- c(5999)
 
# Creating new row
new_row <- data.frame(Product,Size,
                      Color,Price)
print(new_row)
# Append new row in CSV file
data <- rbind(data,new_row)
print(data)


Output:

[1] "File before edit"
  Product   Size Color Price
1   Shirt Medium  Blue  1039
2 T-Shirt  small Green  1899
3   Jeans  large Black  1299
4   Shirt Medium White  1999

[1] "File after edit"
  Product   Size Color Price
1   Shirt Medium  Blue  1039
2 T-Shirt  small Green  1899
3   Jeans  large Black  1299
4  Jacket Medium  Cyan  5999

 Adding and deleting columns in a CSV file

We can delete and add columns in a CSV file using the below method let’s see the code.

R




# Adding column to dataframe
data$quantity <- c(10,20,10,5)
 
# Writing to csv file
write.csv(data,"path to csv file",
          row.names=FALSE)


Explanation: In the above code firstly we are adding a column to the data frame and then writing it into a CSV file.

Adding a quantity column

To delete column from CSV file we used the same method as above but to delete the column we are storing NULL in that column using “$” operator. 

R




# Deleting Size column
data$Size <- NULL
 
# Writing to csv file
write.csv(data,"path to csv file",
          row.names=FALSE)


Output:

 



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads