📖 Scenario: You are working with R data objects and want to save them to a file and then read them back later. This is useful when you want to keep your data safe and load it quickly without recreating it every time.
🎯 Goal: Learn how to save an R object to a file using saveRDS() and then load it back using readRDS().
📋 What You'll Learn
Create a simple R object (a vector) with exact values
Save the object to a file using saveRDS()
Read the object back from the file using readRDS()
Print the loaded object to see the result
💡 Why This Matters
🌍 Real World
Saving and loading R objects is common when working with data analysis projects to avoid repeating expensive computations.
💼 Career
Data scientists and analysts often save intermediate results using <code>saveRDS()</code> and load them later to continue their work efficiently.
Progress0 / 4 steps
1
Create a vector called numbers with values 10, 20, 30, 40, 50
Create a vector called numbers with the exact values 10, 20, 30, 40, 50 using the c() function.
R Programming
Hint
Use c(10, 20, 30, 40, 50) to create the vector and assign it to numbers.
2
Save the numbers vector to a file called numbers.rds using saveRDS()
Use saveRDS() to save the numbers vector to a file named "numbers.rds".
R Programming
Hint
Call saveRDS(numbers, "numbers.rds") to save the vector.
3
Read the vector back from the file numbers.rds into a variable called loaded_numbers using readRDS()
Use readRDS() to read the data from the file "numbers.rds" and assign it to a variable called loaded_numbers.
R Programming
Hint
Use loaded_numbers <- readRDS("numbers.rds") to load the saved vector.
4
Print the loaded_numbers vector to see the output
Write a print() statement to display the contents of loaded_numbers.
R Programming
Hint
Use print(loaded_numbers) to show the vector.
Practice
(1/5)
1. What does the saveRDS() function do in R?
easy
A. It creates a new R script file.
B. It reads data from a CSV file.
C. It saves a single R object to a file.
D. It deletes an R object from memory.
Solution
Step 1: Understand the purpose of saveRDS()
The saveRDS() function is used to save one R object to a file in a binary format.
Step 2: Compare with other options
The other options describe unrelated actions: creating a new R script file, reading data from a CSV file, or deleting an R object from memory.
Final Answer:
It saves a single R object to a file. -> Option C
Quick Check:
saveRDS() = saves object [OK]
Hint: saveRDS saves one object to a file in R [OK]
Common Mistakes:
Confusing saveRDS() with read.csv()
Thinking saveRDS() saves multiple objects at once
Assuming saveRDS() creates scripts
2. Which of the following is the correct syntax to save an object my_data to a file named data.rds?
easy
A. saveRDS(my_data, "data.rds")
B. saveRDS("data.rds", my_data)
C. readRDS(my_data, "data.rds")
D. save(my_data, file = "data.rds")
Solution
Step 1: Recall saveRDS() argument order
The first argument is the object to save, the second is the file name as a string.
Step 2: Check each option
saveRDS(my_data, "data.rds") matches the correct order. saveRDS("data.rds", my_data) reverses arguments. readRDS(my_data, "data.rds") uses readRDS which is for reading, not saving. save(my_data, file = "data.rds") uses save(), which is different.
Final Answer:
saveRDS(my_data, "data.rds") -> Option A
Quick Check:
saveRDS(object, file) = correct syntax [OK]
Hint: Remember: saveRDS(object, "filename") [OK]
Common Mistakes:
Swapping object and filename arguments
Using readRDS() instead of saveRDS() to save
Confusing saveRDS() with save()
3. What will be the output of the following R code?
The filename must be a string, so it needs quotes around it.
Step 2: Identify the error
In readRDS(datafile.rds), the filename is unquoted, causing an error.
Final Answer:
Missing quotes around filename in readRDS() -> Option A
Quick Check:
Filename must be a string in readRDS() [OK]
Hint: Always put filename in quotes in readRDS() [OK]
Common Mistakes:
Forgetting quotes around filename
Confusing saveRDS() and save()
Assuming no error when filename is unquoted
5. You saved a data frame df using saveRDS(df, "df.rds"). Later, you want to load it and add a new column status with value "active" for all rows. Which code correctly does this?
hard
A. df <- saveRDS("df.rds")
df$status <- "active"
B. df <- readRDS("df.rds")
df$status <- "active"
saveRDS(df, "df.rds")
C. readRDS(df, "df.rds")
df$status <- "active"
D. df <- readRDS("df.rds")
addColumn(df, status = "active")
Solution
Step 1: Load the saved data frame
Use df <- readRDS("df.rds") to load the saved object into df.
Step 2: Add new column and save again
Add the new column by df$status <- "active" and save the updated object back with saveRDS(df, "df.rds").