简体   繁体   English

如何将数据文件保存到 .RData 中?

[英]How to save data file into .RData?

I want to save data into an .RData file.我想将数据保存到.RData文件中。

For instance, I'd like to save into 1.RData with two csv files and some information.例如,我想将两个 csv 文件和一些信息保存到1.RData

Here, I have two csv files在这里,我有两个 csv 文件

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.所以,我想我首先需要从两个 csv 文件中创建对象“城市”。

The structure of 1.RData may looks like this: 1.RData 的结构可能如下所示:

> 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. file_1.csvfile_2.csv有一堆行和列。

How can I create this type of RData with csv files and values?如何使用 csv 文件和值创建这种类型的 RData?

Alternatively, when you want to save individual R objects, I recommend using saveRDS .或者,当您想要保存单个 R 对象时,我建议使用saveRDS

You can save R objects using saveRDS , then load them into R with a new variable name using readRDS .您可以使用saveRDS保存 R 对象,然后使用saveRDS将它们加载到具有新变量名的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.但是,当您想在工作区中保存许多/所有对象时,请使用 Manetheran 的答案。

There are three ways to save objects from your R session:有三种方法可以从 R 会话中保存对象:

Saving all objects in your R session:保存 R 会话中的所有对象:

The save.image() function will save all objects currently in your R session: save.image()函数将保存当前在 R 会话中的所有对象:

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

These objects can then be loaded back into a new R session using the load() function:然后可以使用load()函数将这些对象加载回新的 R 会话:

load(file="1.RData")

Saving some objects in your R session:在 R 会话中保存一些对象:

If you want to save some, but not all objects, you can use the save() function:如果你想保存一些但不是所有的对象,你可以使用save()函数:

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

Again, these can be reloaded into another R session using the load() function:同样,这些可以使用load()函数重新加载到另一个 R 会话中:

load(file="1.RData") 

Saving a single object保存单个对象

If you want to save a single object you can use the saveRDS() function:如果要保存单个对象,可以使用saveRDS()函数:

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:您可以使用readRDS()函数将这些加载到 R 会话中,但您需要将结果分配给所需的变量:

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

But this also means you can give these objects new variable names if needed (ie if those variables already exist in your new R session but contain different objects):但这也意味着您可以根据需要为这些对象提供新的变量名称(即,如果这些变量已存在于您的新 R 会话中但包含不同的对象):

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

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这是你可以随时检查它何时运行

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM