Bird
Raised Fist0
R Programmingprogramming~20 mins

readRDS and saveRDS in R Programming - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
R readRDS/saveRDS Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output of this R code using saveRDS and readRDS?

Consider the following R code that saves and reads an object:

my_list <- list(a = 1, b = 2)
saveRDS(my_list, file = "temp.rds")
new_list <- readRDS("temp.rds")
print(new_list$b)

What will be printed?

R Programming
my_list <- list(a = 1, b = 2)
saveRDS(my_list, file = "temp.rds")
new_list <- readRDS("temp.rds")
print(new_list$b)
A2
BNULL
CError: object 'new_list' not found
D1
Attempts:
2 left
💡 Hint

Remember that saveRDS saves the object exactly and readRDS reads it back as is.

🧠 Conceptual
intermediate
1:30remaining
What does saveRDS() do in R?

Which of the following best describes the purpose of saveRDS() in R?

AConverts an R object to a CSV file
BSaves an R object to a file in a binary format for later use
CReads an R object from a file and loads it into the global environment
DSaves all variables in the current environment to a text file
Attempts:
2 left
💡 Hint

Think about what saveRDS() stores and how it differs from save().

🔧 Debug
advanced
2:00remaining
Why does this code cause an error when reading an RDS file?

Look at this R code:

saveRDS(42, file = "number.rds")
num <- readRDS("number.txt")
print(num)

What error will occur and why?

AError: object 'num' not found
BNo error, prints 42
CError: cannot open the connection to 'number.txt' because the file does not exist
DError: invalid R object in 'number.txt'
Attempts:
2 left
💡 Hint

Check the file names used in saving and reading.

📝 Syntax
advanced
2:00remaining
Which option correctly saves and reads an R object?

Which of the following code snippets correctly saves an object df and reads it back into df2?

A
saveRDS(df, "data.rds")
df2 &lt;- readRDS("data.rds")
B
saveRDS(df)
df2 &lt;- readRDS()
C
saveRDS(df, "data.txt")
df2 &lt;- readRDS("data.txt")
D
saveRDS(df, file = "data.rds")
df2 &lt;- readRDS(file = "data.rds")
Attempts:
2 left
💡 Hint

Check the correct argument names and file extensions.

🚀 Application
expert
2:30remaining
How many objects are saved and restored using saveRDS and readRDS?

Given the following R code:

a <- 10
b <- 20
saveRDS(a, file = "a.rds")
saved_obj <- readRDS("a.rds")

How many objects are saved and restored by saveRDS and readRDS?

AOne object: only 'a' is saved and restored
BTwo objects: both 'a' and 'b' are saved and restored
CNo objects are saved because 'b' is not saved explicitly
DAll objects in the environment are saved and restored
Attempts:
2 left
💡 Hint

Remember what saveRDS saves compared to save.

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

  1. Step 1: Understand the purpose of saveRDS()

    The saveRDS() function is used to save one R object to a file in a binary format.
  2. 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.
  3. Final Answer:

    It saves a single R object to a file. -> Option C
  4. 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

  1. Step 1: Recall saveRDS() argument order

    The first argument is the object to save, the second is the file name as a string.
  2. 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.
  3. Final Answer:

    saveRDS(my_data, "data.rds") -> Option A
  4. 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?
my_list <- list(a = 1, b = 2)
saveRDS(my_list, "mylist.rds")
loaded_list <- readRDS("mylist.rds")
print(loaded_list$b)
medium
A. Error: object not found
B. 1
C. NULL
D. 2

Solution

  1. Step 1: Understand saving and loading

    The list my_list has elements a=1 and b=2. It is saved to a file and then loaded back into loaded_list.
  2. Step 2: Access element b of loaded_list

    Accessing loaded_list$b retrieves the value 2, as saved.
  3. Final Answer:

    2 -> Option D
  4. Quick Check:

    readRDS() loads saved object correctly [OK]
Hint: readRDS loads saved object exactly as saved [OK]
Common Mistakes:
  • Expecting print to show 1 instead of 2
  • Thinking readRDS returns NULL
  • Confusing saveRDS and readRDS usage
4. Identify the error in this R code snippet:
saveRDS(data, "datafile.rds")
new_data <- readRDS(datafile.rds)
print(new_data)
medium
A. Missing quotes around filename in readRDS()
B. Using saveRDS() instead of save()
C. Incorrect object name in saveRDS()
D. No error, code runs fine

Solution

  1. Step 1: Check readRDS() argument

    The filename must be a string, so it needs quotes around it.
  2. Step 2: Identify the error

    In readRDS(datafile.rds), the filename is unquoted, causing an error.
  3. Final Answer:

    Missing quotes around filename in readRDS() -> Option A
  4. 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

  1. Step 1: Load the saved data frame

    Use df <- readRDS("df.rds") to load the saved object into df.
  2. 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").
  3. Final Answer:

    df <- readRDS("df.rds") df$status <- "active" saveRDS(df, "df.rds") -> Option B
  4. Quick Check:

    Load, modify, then save again [OK]
Hint: Load with readRDS, modify, then saveRDS to update file [OK]
Common Mistakes:
  • Using saveRDS() to load data
  • Forgetting to save after modification
  • Using incorrect function to add column
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard