What are the main differences between R data files?
Asked Answered
S

2

280

What are the main differences between .RData, .Rda and .Rds files?

  • Are there differences in compression, etc.?
  • When should each type be used?
  • How can one type be converted to another?
Scabious answered 26/1, 2014 at 22:29 Comment(0)
H
232

Rda is just a short name for RData. You can just save(), load(), attach(), etc. just like you do with RData.

Rds stores a single R object. Yet, beyond that simple explanation, there are several differences from a "standard" storage. Probably this R-manual Link to readRDS() function clarifies such distinctions sufficiently.

So, answering your questions:

  • The difference is not about the compression, but serialization (See this page)
  • Like shown in the manual page, you may wanna use it to restore a certain object with a different name, for instance.
  • You may readRDS() and save(), or load() and saveRDS() selectively.
Hinds answered 26/1, 2014 at 22:53 Comment(0)
G
185

In addition to @KenM's answer, another important distinction is that, when loading in a saved object, you can assign the contents of an Rds file. Not so for Rda

> x <- 1:5
> save(x, file="x.Rda")
> saveRDS(x, file="x.Rds")
> rm(x)

## ASSIGN USING readRDS
> new_x1 <- readRDS("x.Rds")
> new_x1
[1] 1 2 3 4 5

## 'ASSIGN' USING load -- note the result
> new_x2 <- load("x.Rda")
loading in to  <environment: R_GlobalEnv> 
> new_x2
[1] "x"
# NOTE: `load()` simply returns the name of the objects loaded. Not the values. 
> x
[1] 1 2 3 4 5
Glissade answered 26/1, 2014 at 23:21 Comment(2)
@HarlanNelson tried it. Did exactly what I expect. What's your point?Debtor
The point is load() simply returns the name of the objects loaded. Not the values . That's a big different with readRDS, rda's load will change something even you don't want, but if you just want a overwrite, then load rda.Backset

© 2022 - 2024 — McMap. All rights reserved.