list 如何将列表保存到文件并再次读取(在 R 中)?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31893844/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can i save a list to a file and read it in again (in R)?
提问by SEMson
I′ve estimated a model with the poLCA-package in R and want to save the starting values to a file, so i can re-estimate exactly the same model anytime.
我已经用 R 中的 poLCA 包估计了一个模型,并希望将起始值保存到一个文件中,以便我可以随时重新估计完全相同的模型。
This is a list of starting values for one model:
这是一个模型的起始值列表:
List of 8
$ : num [1:2, 1:6] 0.219 0.193 0.16 0.193 0.184 ...
$ : num [1:2, 1:6] 0.0731 0.2054 0.228 0.144 0.2028 ...
$ : num [1:2, 1:6] 0.0396 0.0965 0.0286 0.1494 0.1609 ...
$ : num [1:2, 1:6] 0.20998 0.173634 0.105792 0.000588 0.06236 ...
$ : num [1:2, 1:6] 0.163 0.19 0.167 0.178 0.246 ...
$ : num [1:2, 1:6] 0.1602 0.1438 0.1963 0.0848 0.2218 ...
$ : num [1:2, 1:6] 0.0298 0.3022 0.2179 0.094 0.0228 ...
$ : num [1:2, 1:6] 0.0167 0.2444 0.3257 0.1298 0.3652 ...
And these are all its values:
这些都是它的价值:
> starting.values
[[1]]
[,1] [,2] [,3] [,4] [,5] [,6]
[1,] 0.2188410 0.1602971 0.18446855 0.002413188 0.1841924 0.2497878
[2,] 0.1927328 0.1926757 0.04098356 0.104583224 0.1583117 0.3107131
[[2]]
[,1] [,2] [,3] [,4] [,5] [,6]
[1,] 0.07310624 0.2280248 0.2027862 0.2274362 0.03105063 0.2375959
[2,] 0.20535603 0.1439554 0.1869197 0.1317791 0.20698352 0.1250063
[[3]]
[,1] [,2] [,3] [,4] [,5] [,6]
[1,] 0.03955110 0.02855405 0.1609203 0.3375032 0.15405189 0.2794195
[2,] 0.09650825 0.14942635 0.1016048 0.2445582 0.07646363 0.3314387
[[4]]
[,1] [,2] [,3] [,4] [,5] [,6]
[1,] 0.2099798 0.1057921697 0.06235958 0.06833102 0.2474372 0.3061002
[2,] 0.1736344 0.0005879314 0.06184313 0.36905589 0.2575882 0.1372904
[[5]]
[,1] [,2] [,3] [,4] [,5] [,6]
[1,] 0.1631299 0.1672565 0.2460589 0.2199485 0.1620184 0.04158786
[2,] 0.1900245 0.1777367 0.1136598 0.1576786 0.1147886 0.24611175
[[6]]
[,1] [,2] [,3] [,4] [,5] [,6]
[1,] 0.1601707 0.19628931 0.2217799 0.1985856 0.1961983 0.02697623
[2,] 0.1437703 0.08483575 0.3475932 0.1029784 0.2134874 0.10733507
[[7]]
[,1] [,2] [,3] [,4] [,5] [,6]
[1,] 0.0297938 0.21786564 0.02278498 0.2173179 0.28299340 0.2292443
[2,] 0.3021657 0.09397824 0.16714915 0.3072889 0.02752554 0.1018925
[[8]]
[,1] [,2] [,3] [,4] [,5] [,6]
[1,] 0.01670364 0.3256942 0.3652010 0.1620259 0.1111144 0.01926083
[2,] 0.24443214 0.1297942 0.3064312 0.1105086 0.1461748 0.06265905
First i thought, that i can convert the list to a dataframe and save it as a .csv-file. But i don′t know, how i would read-in this file to have exactly the same list, that i get from the model. I already looked around stackoverflow, but didn′t find the answer to my question. I don′t mind if the file would be .csv, .txt or if it′s one file or many. I′m sorry, if i can′t offer any more ideas or code for my question. I don′t know where to start. Thank you for your help!
首先我想,我可以将列表转换为数据框并将其保存为 .csv 文件。但我不知道,我将如何读入这个文件以获得与我从模型中获得的完全相同的列表。我已经环顾了 stackoverflow,但没有找到我的问题的答案。我不介意文件是 .csv、.txt 还是一个文件或多个文件。对不起,如果我不能为我的问题提供更多的想法或代码。我不知道从哪里开始。感谢您的帮助!
采纳答案by Whitebeard
save(starting.values, file="fname.RData")
?save
Allows you to save one or more R objects to a single file.
允许您将一个或多个 R 对象保存到单个文件中。
Load with
加载
load("fname.RData")
回答by Konrad
I would also suggest that you explore rlist
package and the offered list.save
function as it provides additional flexibility when saving lists.
我还建议您探索rlist
包和提供的list.save
功能,因为它在保存列表时提供了额外的灵活性。
Example
例子
As available in documentation.
如文档中所述。
require(rlist)
x <- lapply(1:5,function(i) data.frame(a=i,b=i^2))
list.save(x, 'list.rds')
list.save(x, 'list.rdata')
list.save(x, 'list.yaml')
yaml
file preview
yaml
文件预览
- a: 1
b: 1.0
- a: 2
b: 4.0
- a: 3
b: 9.0
- a: 4
b: 16.0
- a: 5
b: 25.0
You could then load list with use of the list.load
function in the same package or you could read lines from the exported yaml
file. You could also consider having a look at the list.select
function and selecting objects from within list. Having said that, I presume that the easiest way to approach this would be simply to store/load a full list object.
然后,您可以使用list.load
同一包中的函数加载列表,或者您可以从导出的yaml
文件中读取行。您还可以考虑查看list.select
功能并从列表中选择对象。话虽如此,我认为解决这个问题的最简单方法就是存储/加载一个完整的列表对象。