📖 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
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
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
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
Hint
Use write.csv(sales_data, file = "updated_sales.csv", row.names = FALSE) and print(sales_data).
Practice
(1/5)
1. What does the read.csv function do in R?
easy
A. Loads data from a CSV file into a data frame
B. Saves a data frame to a CSV file
C. Creates a new CSV file with default data
D. Deletes a CSV file from the disk
Solution
Step 1: Understand the purpose of read.csv
The read.csv function is designed to read data stored in CSV files and load it into R as a data frame.
Step 2: Differentiate from write.csv
write.csv saves data frames to CSV files, so it does the opposite of read.csv.
Final Answer:
Loads data from a CSV file into a data frame -> Option A
2. Which of the following is the correct syntax to save a data frame named df to a CSV file called output.csv?
easy
A. read.csv(df, file = "output.csv")
B. write.csv(df, file = "output.csv")
C. write.csv(file = df, "output.csv")
D. write.csv("output.csv", df)
Solution
Step 1: Recall the argument order of write.csv
The first argument is the data frame to save, the second is the file name with the parameter file=.
Step 2: Check each option's argument order
write.csv(df, file = "output.csv") correctly uses write.csv(df, file = "output.csv"). read.csv(df, file = "output.csv") uses read.csv which is for reading, not writing. Options B and D have wrong argument order.
Final Answer:
write.csv(df, file = "output.csv") -> Option B
Quick Check:
write.csv(data, file=filename) syntax [OK]
Hint: write.csv(data, file=filename) saves data frame [OK]
Common Mistakes:
Swapping arguments order
Using read.csv instead of write.csv
Omitting the file= parameter
3. What will be the output of the following R code?
B. write.csv should not have data frame as first argument
C. Missing quotes around file name in read.csv
D. read.csv cannot read CSV files
Solution
Step 1: Check the write.csv usage
write.csv(df, "data.csv") is correct syntax to save the data frame.
Step 2: Check the read.csv usage
The code uses read.csv(data.csv) without quotes around the file name, which causes an error because R treats data.csv as a variable, not a string.
Final Answer:
Missing quotes around file name in read.csv -> Option C
Quick Check:
File names must be strings in quotes [OK]
Hint: Always put file names in quotes in read.csv and write.csv [OK]
Common Mistakes:
Omitting quotes around file names
Confusing argument order
Thinking print() can't show data frames
5. You have a CSV file with missing values represented as empty cells. Which read.csv argument helps you automatically convert these empty cells to NA in R?
hard
A. na.strings = ""
B. stringsAsFactors = TRUE
C. header = FALSE
D. row.names = NULL
Solution
Step 1: Understand missing values in CSV
Empty cells in CSV files often represent missing data, which R can mark as NA if told how to interpret them.
Step 2: Use na.strings argument
The na.strings = "" argument tells read.csv to treat empty strings as NA values automatically.
Final Answer:
na.strings = "" -> Option A
Quick Check:
na.strings="" converts empty cells to NA [OK]
Hint: Use na.strings="" to read blanks as NA [OK]
Common Mistakes:
Ignoring na.strings argument
Using stringsAsFactors which affects factor conversion
Confusing header or row.names with missing data handling
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.