0
0
R Programmingprogramming~30 mins

read.csv and write.csv in R Programming - Mini Project: Build & Apply

Choose your learning style9 modes available
Reading and Writing CSV Files in R
📖 Scenario: You work in a small shop and keep track of your sales data in a CSV file. You want to load this data into R, make some changes, and then save the updated data back to a new CSV file.
🎯 Goal: Learn how to use read.csv to load data from a CSV file and write.csv to save data back to a CSV file in R.
📋 What You'll Learn
Create a CSV file named sales.csv with given data
Use read.csv to load the CSV file into a variable
Add a new column to the data frame
Use write.csv to save the updated data to a new CSV file
💡 Why This Matters
🌍 Real World
CSV files are a common way to store and share data like sales, inventory, or survey results. Being able to read and write CSV files lets you work with data easily in R.
💼 Career
Many jobs in data analysis, business, and science require handling CSV files. Knowing how to use read.csv and write.csv is a basic but important skill.
Progress0 / 4 steps
1
Create the CSV file sales.csv
Create a CSV file named sales.csv with these exact contents (including header):
Product,Quantity,Price
Apple,10,0.5
Banana,5,0.3
Cherry,20,0.2
R Programming
Need a hint?

Use write.csv with row.names = FALSE to create the file.

2
Load the CSV file into a variable
Use read.csv to read the file sales.csv into a variable called sales_data.
R Programming
Need a hint?

Use sales_data <- read.csv("sales.csv") to load the data.

3
Add a new column to the data frame
Add a new column called Total to sales_data that multiplies Quantity by Price.
R Programming
Need a hint?

Use sales_data$Total <- sales_data$Quantity * sales_data$Price to create the new column.

4
Save the updated data to a new CSV file
Use write.csv to save sales_data to a new file called updated_sales.csv without row names. Then print the contents of sales_data.
R Programming
Need a hint?

Use write.csv(sales_data, file = "updated_sales.csv", row.names = FALSE) and print(sales_data).