在单个 RDS 文件中保存多个变量 [英] Saving several variables in a single RDS file

查看:81
本文介绍了在单个 RDS 文件中保存多个变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将变量列表传递给 saveRDS() 以保存它们的值,但它会保存它们的名称:

I want to pass a list of variables to saveRDS() to save their values, but instead it saves their names:

variables <- c("A", "B", "C")
saveRDS(variables, "file.R")

它保存了单个向量变量".

it saves the single vector "variables".

我也试过:

save(variables, "file.RData")

没有成功

推荐答案

您需要使用 save 函数的 list 参数.EG:

You need to use the list argument of the save function. EG:

var1 = "foo"
var2 = 2
var3 = list(a="abc", z="xyz")
ls()
save(list=c("var1", "var2", "var3"), file="myvariables.RData")
rm(list=ls())
ls()

load("myvariables.RData")
ls()

请注意,saveRDS 函数会创建一个 .RDS 文件,用于保存单个 R 对象.save 函数创建一个 .RData 文件(与 .RDA 文件相同)..RData 文件用于存储整个 R 工作区,或者将 R 工作区中的任何名称传递给 list 参数.

Please note that the saveRDS function creates a .RDS file, which is used to save a single R object. The save function creates a .RData file (same thing as .RDA file). .RData files are used to store an entire R workspace, or whichever names in an R workspace are passed to the list argument.

YiHui 有一篇关于这个主题的不错的博文.

YiHui has a nice blogpost on this topic.

如果您有多个数据表并且需要将它们全部保存在单个 R 对象中,那么您可以使用 saveRDS 路线.举个例子:

If you have several data tables and need them all saved in a single R object, then you can go the saveRDS route. As an example:

datalist = list(mtcars = mtcars, pressure=pressure)
saveRDS(datalist, "twodatasets.RDS")
rm(list=ls())

datalist = readRDS("twodatasets.RDS")
datalist

这篇关于在单个 RDS 文件中保存多个变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆