Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
496 views
in Technique[技术] by (71.8m points)

r - How to save data file into .RData?

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...