I have three dataset saved in R format A.RData, B.RData, C.RData (each of the size of ~2Gb). Each of the files contains three variables X, Y, Z.
我有三个数据集保存在R格式A.RData,B.RData,C.RData(每个大小~2Gb)。每个文件包含三个变量X,Y,Z。
I can not load A.RData and B.RData without first renaming the variables. As the datasets are big these steps:
在没有首先重命名变量的情况下,我无法加载A.RData和B.RData。由于数据集很大,这些步骤:
load("A.RData")
A = list(X=X,Y=Y,Z=Z)
rm(X,Y,Z)
load("B.RData")
B = list(X=X,Y=Y,Z=Z)
rm(X,Y,Z)
take some time.
需要一些时间。
Is there a way to import the data from A.RData directly in a list A, without having to make copies of the variable?
有没有办法直接在列表A中导入A.RData中的数据,而无需复制变量?
2 个解决方案
#1
1
Yes, there is.
就在这里。
A <- new.env()
load('A.RData', envir=A)
A <- as.list(A)
#2
0
The solution provided by Matthew Plourde is the way to go. In future, you can avoid these problems if you save your data with saveRDS
(not save
).
Matthew Plourde提供的解决方案是可行的方法。将来,如果使用saveRDS(而不是保存)保存数据,则可以避免这些问题。
The function saveRDS
saves a single object. For example:
saveRDS函数保存单个对象。例如:
X <- 1
Y <- 2
Z <- 3
# put the objects in a list
mylist <- list(X, Y, Z)
# save the list
saveRDS(mylist, file = "myfile.rds")
rm(X, Y, Z, mylist) # remove objects (not necessary)
# load the list
newlist <- readRDS("myfile.rds")
# [[1]]
# [1] 1
#
# [[2]]
# [1] 2
#
# [[3]]
# [1] 3
In contrast to save
, the name of the object is not stored. Hence, you can assign the result of readRDS
to another name. Note that you have to put all variables in one list.
与save相反,不存储对象的名称。因此,您可以将readRDS的结果分配给另一个名称。请注意,您必须将所有变量放在一个列表中。
#1
1
Yes, there is.
就在这里。
A <- new.env()
load('A.RData', envir=A)
A <- as.list(A)
#2
0
The solution provided by Matthew Plourde is the way to go. In future, you can avoid these problems if you save your data with saveRDS
(not save
).
Matthew Plourde提供的解决方案是可行的方法。将来,如果使用saveRDS(而不是保存)保存数据,则可以避免这些问题。
The function saveRDS
saves a single object. For example:
saveRDS函数保存单个对象。例如:
X <- 1
Y <- 2
Z <- 3
# put the objects in a list
mylist <- list(X, Y, Z)
# save the list
saveRDS(mylist, file = "myfile.rds")
rm(X, Y, Z, mylist) # remove objects (not necessary)
# load the list
newlist <- readRDS("myfile.rds")
# [[1]]
# [1] 1
#
# [[2]]
# [1] 2
#
# [[3]]
# [1] 3
In contrast to save
, the name of the object is not stored. Hence, you can assign the result of readRDS
to another name. Note that you have to put all variables in one list.
与save相反,不存储对象的名称。因此,您可以将readRDS的结果分配给另一个名称。请注意,您必须将所有变量放在一个列表中。