How to edit CSV files in R
Last Updated :
27 Jan, 2023
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:
CSV FileReading 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:
Similar Reads
How to Read CSV File in Ruby?
It is common to have tabular data stored in CSV (Comma Separated Values) files. In Ruby, one can handle CSV files without much ado because the built-in libraries make it easy to do so. This article focuses on discussing the ways to read a CSV file in Ruby. Approach to Read CSV Files in Ruby?There ar
2 min read
How to delete a CSV file in Python?
In this article, we are going to delete a CSV file in Python. CSV (Comma-separated values file) is the most commonly used file format to handle tabular data. The data values are separated by, (comma). The first line gives the names of the columns and after the next line the values of each column. Ap
2 min read
How to read excel file in R ?
We may work with structured data from spreadsheets, take advantage of R's capabilities for data analysis and manipulation, and incorporate Excel data into other R processes and packages by reading Excel files in R. The readxl package offers a simple and effective method for reading Excel files into
3 min read
How to Import .dta Files into R?
In this article, we will discuss how to import .dta files in the R Programming Language.There are many types of files that contain datasets, for example, CSV, Excel file, etc. These are used extensively with the R Language to import or export data sets into files. One such format is DAT which is sav
2 min read
How to parse a CSV File in PHP ?
In this article, we learn to parse a CSV file using PHP code, along with understanding its basic implementation through examples. Â Approach: The following approach will be utilized to parse the CSV File using PHP, which is described below: Step 1. Add data to an Excel file. The following example is
3 min read
How to Import a CSV File into R ?
A CSV file is used to store contents in a tabular-like format, which is organized in the form of rows and columns. The column values in each row are separated by a delimiter string. The CSV files can be loaded into the working space and worked using both in-built methods and external package imports
3 min read
Writing to CSV files in R
For Data Analysis sometimes creating CSV data file is required and do some operations on it as per our requirement. So, In this article we are going to learn that how to write data to CSV File using R Programming Language. To write to csv file write.csv() function is used. Syntax: write.csv(data, pa
1 min read
How to create dataframe in R
Dataframes are fundamental data structures in R for storing and manipulating data in tabular form. They allow you to organize data into rows and columns, similar to a spreadsheet or a database table. Creating a data frame in the R Programming Language is a simple yet essential task for data analysis
3 min read
How to Export DataFrame to CSV in R ?
R Programming language allows us to read and write data into various files like CSV, Excel, XML, etc. In this article, we are going to discuss how to Export DataFrame to CSV file in R Programming Language. Approach:Â Write Data in column wise formatCreate DataFrame for these dataWrite Data to the CS
1 min read
How to Read Many ASCII Files into R?
Reading data from ASCII files into R is a common task in data analysis and statistical computing. ASCII files, known for their simplicity and wide compatibility, often contain text data that can be easily processed in R. Here we read multiple ASCII files into R Programming Language. What are ASCII F
4 min read