85

I want to save data into an .RData file.

For instance, I'd like to save into 1.RData with two csv files and some information.

Here, I have two csv files

1) file_1.csv contains object city[[1]]
2) file_2.csv contains object city[[2]]

and additionally save other values, country and population as follows. So, I guess I need to make objects 'city' from two csv files first of all.

The structure of 1.RData may looks like this:

> data = load("1.RData")

> data
[1] "city"  "country"  "population"

> city
  [[1]]               
  NEW YORK         1.1
  SAN FRANCISCO    3.1

  [[2]]
  TEXAS            1.3
  SEATTLE          1.4

> class(city)
  [1] "list"

> country
  [1] "east"  "west"  "north"

> class(country)
  [1] "character"

> population
  [1] 10  11  13  14   

> class(population)
  [1] "integer"

file_1.csv and file_2.csv have bunch of rows and columns.

How can I create this type of RData with csv files and values?

Eric Leschinski
  • 146,994
  • 96
  • 417
  • 335
user2913161
  • 1,115
  • 2
  • 13
  • 15

3 Answers3

102

Alternatively, when you want to save individual R objects, I recommend using saveRDS.

You can save R objects using saveRDS, then load them into R with a new variable name using readRDS.

Example:

# Save the city object
saveRDS(city, "city.rds")

# ...

# Load the city object as city
city <- readRDS("city.rds")

# Or with a different name
city2 <- readRDS("city.rds")

But when you want to save many/all your objects in your workspace, use Manetheran's answer.

ialm
  • 8,510
  • 4
  • 36
  • 48
  • Interesting. I wasn't aware of `saveRDS` even though it's also base R... I have always been using Manetheran's solution. – cryo111 Nov 14 '13 at 01:53
  • 3
    I don’t know why this isn’t more widely recommended / known. It’s usually a much better solution than `save` / `load`. – Konrad Rudolph May 28 '14 at 17:09
  • 2
    @KonradRudolph Why is `saveRDS` better? With `save/load` you use `save(object, file)` and `newObject <- load(file)`. With RDS you use `saveRDS(object, file)` and `newObject <- readRDS(file)`. They seem similar to me, but for the few extra keystrokes using `saveRDS` and `readRDS`. I assume I am missing something. – Dr. Beeblebrox Oct 23 '14 at 15:04
  • 12
    @jabberwocky `load` by default dumps objects into your (global) environment. That’s a terrible idea. You *want* the control that `readRDS` gives you. R’s `load` is actively promoting bad practice here. – Konrad Rudolph Oct 23 '14 at 16:11
  • @KonradRudolph Again, I assume I'm missing something. I don't see `load` clearing the environment. I created an SSCC example. The code is awkward in the comment here, but here it goes: `test <- cbind(1:10, 11:20); save(test, file = "example.Rdata"); remove(list = ls()); load("example.Rdata"); test1 <- 1:10; test2 <- 11:20; ls(); ls(envir=.GlobalEnv); load("example.Rdata"); ls(); ls(envir=.GlobalEnv); #Load does not remove objects from environment. ie test1 and test2 are still there`. – Dr. Beeblebrox Oct 23 '14 at 19:38
  • 5
    @jabberwocky Think about this scenario: say you save your matrix, `test` with `save(test, file = "example.Rdata")`. Then you later define `test` as something else (say, `test <- c(1:100)`). What happens when you do `load("example.Rdata")`? It will replace your newly defined `test` variable. Another scenario: what if you need the data that you saved in `example.rdata`, but want to keep the newly defined `test` variable. Can you *easily* load `example.Rdata` into a new variable, say `test3` with `load`? – ialm Oct 23 '14 at 19:44
  • 2
    The utility that I find in `save` is to save my current environment in R, but to save individual R objects, I find `saveRDS` and `readRDS` are much better. – ialm Oct 23 '14 at 19:46
91

There are three ways to save objects from your R session:

Saving all objects in your R session:

The save.image() function will save all objects currently in your R session:

save.image(file="1.RData") 

These objects can then be loaded back into a new R session using the load() function:

load(file="1.RData")

Saving some objects in your R session:

If you want to save some, but not all objects, you can use the save() function:

save(city, country, file="1.RData")

Again, these can be reloaded into another R session using the load() function:

load(file="1.RData") 

Saving a single object

If you want to save a single object you can use the saveRDS() function:

saveRDS(city, file="city.rds")
saveRDS(country, file="country.rds") 

You can load these into your R session using the readRDS() function, but you will need to assign the result into a the desired variable:

city <- readRDS("city.rds")
country <- readRDS("country.rds")

But this also means you can give these objects new variable names if needed (i.e. if those variables already exist in your new R session but contain different objects):

city_list <- readRDS("city.rds")
country_vector <- readRDS("country.rds")
Juha Palomäki
  • 26,385
  • 2
  • 38
  • 43
Scott Ritchie
  • 10,293
  • 3
  • 28
  • 64
1

Just to add an additional function should you need it. You can include a variable in the named location, for example a date identifier

date <- yyyymmdd
save(city, file=paste0("c:\\myuser\\somelocation\\",date,"_RData.Data")

This was you can always keep a check of when it was run

Sajjad M
  • 35
  • 4