readRDS and saveRDS in R Programming - Time & Space Complexity
When working with files in R, it's important to know how the time to read or save data changes as the data size grows.
We want to understand how the time cost grows when using readRDS and saveRDS functions.
Analyze the time complexity of the following code snippet.
# Save an object to a file
saveRDS(my_data, file = "data.rds")
# Read the object back from the file
loaded_data <- readRDS("data.rds")
This code saves an R object to disk and then reads it back into memory.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Reading or writing each element of the object to or from disk.
- How many times: Once for each element or data unit inside the object, depending on its size.
As the size of the data object grows, the time to save or read it grows roughly in direct proportion.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 units processed |
| 100 | 100 units processed |
| 1000 | 1000 units processed |
Pattern observation: Doubling the data size roughly doubles the time needed to read or save.
Time Complexity: O(n)
This means the time to read or save grows linearly with the size of the data.
[X] Wrong: "Reading or saving small files takes the same time as big files."
[OK] Correct: The time depends on how much data is processed; bigger files take more time because more data is handled.
Understanding how file operations scale helps you reason about program speed and efficiency in real projects.
"What if we compressed the data before saving? How would the time complexity change?"
Practice
saveRDS() function do in R?Solution
Step 1: Understand the purpose of saveRDS()
ThesaveRDS()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 CQuick Check:
saveRDS() = saves object [OK]
- Confusing saveRDS() with read.csv()
- Thinking saveRDS() saves multiple objects at once
- Assuming saveRDS() creates scripts
my_data to a file named 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 AQuick Check:
saveRDS(object, file) = correct syntax [OK]
- Swapping object and filename arguments
- Using readRDS() instead of saveRDS() to save
- Confusing saveRDS() with save()
my_list <- list(a = 1, b = 2)
saveRDS(my_list, "mylist.rds")
loaded_list <- readRDS("mylist.rds")
print(loaded_list$b)Solution
Step 1: Understand saving and loading
The listmy_listhas elements a=1 and b=2. It is saved to a file and then loaded back intoloaded_list.Step 2: Access element b of loaded_list
Accessingloaded_list$bretrieves the value 2, as saved.Final Answer:
2 -> Option DQuick Check:
readRDS() loads saved object correctly [OK]
- Expecting print to show 1 instead of 2
- Thinking readRDS returns NULL
- Confusing saveRDS and readRDS usage
saveRDS(data, "datafile.rds") new_data <- readRDS(datafile.rds) print(new_data)
Solution
Step 1: Check readRDS() argument
The filename must be a string, so it needs quotes around it.Step 2: Identify the error
InreadRDS(datafile.rds), the filename is unquoted, causing an error.Final Answer:
Missing quotes around filename in readRDS() -> Option AQuick Check:
Filename must be a string in readRDS() [OK]
- Forgetting quotes around filename
- Confusing saveRDS() and save()
- Assuming no error when filename is unquoted
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?Solution
Step 1: Load the saved data frame
Usedf <- readRDS("df.rds")to load the saved object intodf.Step 2: Add new column and save again
Add the new column bydf$status <- "active"and save the updated object back withsaveRDS(df, "df.rds").Final Answer:
df <- readRDS("df.rds") df$status <- "active" saveRDS(df, "df.rds") -> Option BQuick Check:
Load, modify, then save again [OK]
- Using saveRDS() to load data
- Forgetting to save after modification
- Using incorrect function to add column
Ready to prove it?
Pass the test and this pattern moves unknown
