如何将列表保存到文件并再次读取

如何将列表保存到文件并再次读取

本文介绍了如何将列表保存到文件并再次读取(在 R 中)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用 R 中的 poLCA 包估计了一个模型,并希望将起始值保存到一个文件中,以便我可以随时重新估计完全相同的模型.

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.

这是一个模型的起始值列表:

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 ...

这些都是它的价值:

> 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

首先我想,我可以将列表转换为数据框并将其保存为 .csv 文件.但我不知道,我将如何读入这个文件以获得与我从模型中获得的完全相同的列表.

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.

我已经查看了 stackoverflow,但没有找到我的问题的答案.我不介意文件是 .csv、.txt 还是一个文件或多个文件.对不起,如果我不能为我的问题提供更多想法或代码.我不知道从哪里开始.

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 am sorry, if I can't offer any more ideas or code for my question. I don't know where to start.

推荐答案

saveRDS(starting.values, file="fname.RData")
?saveRDS

允许您将一个或多个 R 对象保存到单个文件中.

Allows you to save one or more R objects to a single file.

加载

readRDS("fname.RData")

(根据下面的评论编辑,因为之前的 saveload 函数似乎不再起作用了.)

(Edited from comments below, since the previous save and load functions don't seem to work anymore.)

这篇关于如何将列表保存到文件并再次读取(在 R 中)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-25 13:23