保存/导出输出列表以供将来在R中使用?

时间:2017-09-15 17:13:39

标签: r list save export

我在R中进行模拟,需要一段时间才能运行。现在,此模拟仅返回原始输出。我还在开发一个函数,它可以独立于模拟本身操作和汇总这个原始输出。

现在,当我开始R的新会话时(有时我需要重新启动计算机),我必须从模拟中创建新输出,以便继续处理我的单独汇总功能。我想知道是否有一种方法可以导出我的模拟输出,这样我每次开始新的R会话时都可以引用它?

我熟悉write.csv,但我只将其用于单个dfs或矩阵。我的模拟输出是由数据帧组成的列表。我试过谷歌搜索,但我不知道如何导出这个输出列表,以便我可以简单地将其读入R以备将来使用。

以下是我的模拟输出的示例:

$`1_1`
$`1_1`[[1]]
         mean     sd MC_error val2.5pc median val97.5pc start sample
RR[1]  0.7019 0.2361 0.004283   0.3238 0.6754    1.2360  1001   4000
RR[2]  0.8272 0.2573 0.004271   0.4072 0.7981    1.3980  1001   4000
RR[3]  2.1340 0.6723 0.012830   1.0910 2.0320    3.7000  1001   4000
RR[4]  0.9561 0.2975 0.005175   0.4807 0.9185    1.6460  1001   4000
RR[5]  1.0240 0.3072 0.005411   0.5300 0.9912    1.7080  1001   4000
RR[6]  1.5390 0.4806 0.008306   0.7824 1.4810    2.6890  1001   4000
RR[7]  0.9261 0.2895 0.004738   0.4531 0.8941    1.5750  1001   4000
RR[8]  2.0830 0.5898 0.011580   1.1190 2.0080    3.4470  1001   4000
RR[9]  0.9283 0.2938 0.004105   0.4571 0.8954    1.6020  1001   4000
RR[10] 0.8101 0.2621 0.004615   0.3779 0.7842    1.3950  1001   4000
RR[11] 0.6641 0.2096 0.003801   0.3161 0.6428    1.1400  1001   4000
RR[12] 1.2790 0.3620 0.005807   0.6876 1.2410    2.1130  1001   4000
RR[13] 1.5350 0.5261 0.008755   0.7415 1.4590    2.7700  1001   4000
RR[14] 3.0160 1.1210 0.025830   1.3570 2.8270    5.6150  1001   4000
RR[15] 1.3960 0.4376 0.006281   0.6850 1.3500    2.3730  1001   4000
RR[16] 0.8216 0.2668 0.004341   0.3825 0.7924    1.4260  1001   4000
RR[17] 1.1150 0.3155 0.004257   0.5949 1.0820    1.8100  1001   4000
RR[18] 1.0240 0.3180 0.005216   0.5246 0.9866    1.7470  1001   4000
RR[19] 1.3870 0.3618 0.005552   0.7793 1.3550    2.1780  1001   4000
RR[20] 1.0360 0.3169 0.005407   0.5292 1.0020    1.7410  1001   4000
RR[21] 1.1380 0.3255 0.004510   0.6100 1.1060    1.8590  1001   4000
RR[22] 1.2520 0.3837 0.005944   0.6375 1.2030    2.1350  1001   4000
RR[23] 1.0130 0.2606 0.004266   0.5661 0.9903    1.5970  1001   4000
RR[24] 0.9086 0.2891 0.004517   0.4366 0.8716    1.5670  1001   4000
RR[25] 1.3420 0.3837 0.006165   0.7141 1.3040    2.2070  1001   4000
RR[26] 0.9877 0.2872 0.004642   0.5139 0.9541    1.6260  1001   4000
RR[27] 0.9767 0.2295 0.003219   0.5909 0.9524    1.4800  1001   4000
RR[28] 0.6554 0.1892 0.003494   0.3407 0.6351    1.0690  1001   4000
RR[29] 1.1270 0.3399 0.004825   0.5813 1.0850    1.9360  1001   4000
RR[30] 1.1600 0.2785 0.004405   0.6970 1.1360    1.7730  1001   4000
RR[31] 1.1620 0.3096 0.004316   0.6481 1.1320    1.8790  1001   4000
RR[32] 0.9623 0.3105 0.004872   0.4758 0.9279    1.6850  1001   4000
RR[33] 1.0850 0.3242 0.005618   0.5678 1.0480    1.8170  1001   4000
RR[34] 1.1970 0.3093 0.004674   0.6799 1.1730    1.8770  1001   4000
RR[35] 0.9759 0.2696 0.003909   0.5325 0.9473    1.5920  1001   4000
RR[36] 1.0150 0.2836 0.004981   0.5353 0.9851    1.6600  1001   4000
RR[37] 0.5347 0.1565 0.003182   0.2735 0.5193    0.8818  1001   4000
RR[38] 0.4884 0.1536 0.003391   0.2371 0.4724    0.8368  1001   4000
RR[39] 0.6822 0.1711 0.002677   0.3878 0.6670    1.0590  1001   4000
RR[40] 0.9158 0.2704 0.003893   0.4786 0.8891    1.5380  1001   4000
RR[41] 1.5450 0.4564 0.006619   0.8101 1.4900    2.5720  1001   4000
RR[42] 0.9248 0.2964 0.004344   0.4559 0.8896    1.6150  1001   4000
RR[43] 0.5465 0.1764 0.003420   0.2596 0.5322    0.9464  1001   4000
RR[44] 0.9240 0.2930 0.004803   0.4516 0.8893    1.5960  1001   4000

$`1_1`[[2]]
         mean     sd MC_error val2.5pc median val97.5pc start sample
RR[1]  0.8063 0.2904 0.004716   0.3603 0.7689    1.4900  1001   4000
RR[2]  0.5037 0.2004 0.003569   0.1930 0.4809    0.9754  1001   4000
RR[3]  1.9320 0.6532 0.011190   0.8829 1.8500    3.4980  1001   4000
RR[4]  1.0250 0.3362 0.006026   0.4891 0.9846    1.7930  1001   4000
RR[5]  0.5143 0.2125 0.004398   0.1936 0.4836    1.0200  1001   4000
RR[6]  1.6060 0.5191 0.008263   0.7718 1.5490    2.7840  1001   4000
RR[7]  0.7761 0.2914 0.004174   0.3291 0.7373    1.4710  1001   4000
RR[8]  1.2780 0.4432 0.006818   0.5752 1.2210    2.3230  1001   4000
RR[9]  0.7724 0.2846 0.004568   0.3221 0.7460    1.4220  1001   4000
RR[10] 0.9518 0.3186 0.004953   0.4447 0.9199    1.6910  1001   4000
RR[11] 0.4497 0.1785 0.003439   0.1721 0.4268    0.8686  1001   4000
RR[12] 0.6595 0.2587 0.004312   0.2661 0.6256    1.2830  1001   4000
RR[13] 1.2280 0.5014 0.008534   0.4827 1.1470    2.4020  1001   4000
RR[14] 3.2500 1.2120 0.022290   1.3560 3.0580    6.1720  1001   4000
RR[15] 1.4540 0.4913 0.008027   0.6922 1.3820    2.5760  1001   4000
RR[16] 0.8626 0.3058 0.004241   0.3898 0.8212    1.5680  1001   4000
RR[17] 1.2030 0.3592 0.006269   0.6188 1.1670    2.0110  1001   4000
RR[18] 1.4940 0.4459 0.007102   0.7733 1.4400    2.5210  1001   4000
RR[19] 0.9936 0.3191 0.004616   0.4789 0.9538    1.7250  1001   4000
RR[20] 0.5828 0.2432 0.004339   0.2096 0.5501    1.1560  1001   4000
RR[21] 0.9172 0.3047 0.005035   0.4227 0.8818    1.6240  1001   4000
RR[22] 0.6529 0.2804 0.004139   0.2383 0.6096    1.3180  1001   4000
RR[23] 0.9142 0.2679 0.004130   0.4583 0.8893    1.5010  1001   4000
RR[24] 0.9641 0.3227 0.005514   0.4446 0.9264    1.7310  1001   4000
RR[25] 1.3690 0.4135 0.007314   0.6912 1.3190    2.2920  1001   4000
RR[26] 0.6732 0.2451 0.004072   0.2900 0.6404    1.2420  1001   4000
RR[27] 0.6030 0.1840 0.003017   0.2999 0.5827    1.0140  1001   4000
RR[28] 0.6519 0.2004 0.003232   0.3208 0.6310    1.0930  1001   4000
RR[29] 0.6803 0.2744 0.004725   0.2670 0.6403    1.3360  1001   4000
RR[30] 0.5276 0.1847 0.003239   0.2281 0.5076    0.9603  1001   4000
RR[31] 0.9763 0.3013 0.004561   0.4949 0.9466    1.6600  1001   4000
RR[32] 0.5934 0.2481 0.004518   0.2248 0.5596    1.1930  1001   4000
RR[33] 0.6487 0.2554 0.003746   0.2561 0.6084    1.2370  1001   4000
RR[34] 0.6916 0.2441 0.004238   0.3093 0.6602    1.2660  1001   4000
RR[35] 0.5388 0.2026 0.003581   0.2221 0.5105    1.0070  1001   4000
RR[36] 1.2720 0.3578 0.005380   0.6866 1.2370    2.0640  1001   4000
RR[37] 0.6141 0.1802 0.002972   0.3166 0.5950    1.0170  1001   4000
RR[38] 0.4088 0.1441 0.002842   0.1808 0.3901    0.7413  1001   4000
RR[39] 0.4162 0.1381 0.002273   0.1924 0.4022    0.7266  1001   4000
RR[40] 0.7893 0.2603 0.004345   0.3673 0.7597    1.3960  1001   4000
RR[41] 1.0440 0.3824 0.005550   0.4523 0.9927    1.9340  1001   4000
RR[42] 0.8632 0.3058 0.004773   0.3853 0.8158    1.5570  1001   4000
RR[43] 0.6449 0.2061 0.002788   0.3070 0.6185    1.1090  1001   4000
RR[44] 1.2230 0.3901 0.006244   0.5960 1.1840    2.1050  1001   4000


$`1_2a`
$`1_2a`[[1]]
         mean     sd MC_error val2.5pc median val97.5pc start sample
RR[1]  0.7409 0.2534 0.005284   0.3335 0.7159    1.3110  1001   4000
RR[2]  0.8294 0.2733 0.004385   0.3956 0.7962    1.4380  1001   4000
RR[3]  2.2790 0.6923 0.013190   1.1700 2.1990    3.8670  1001   4000
RR[4]  0.9933 0.3063 0.004733   0.4860 0.9608    1.6850  1001   4000
RR[5]  1.0880 0.3294 0.005059   0.5551 1.0460    1.8220  1001   4000
RR[6]  1.6260 0.4889 0.008983   0.8362 1.5620    2.7240  1001   4000
RR[7]  0.9308 0.3019 0.004657   0.4370 0.8980    1.6140  1001   4000
RR[8]  2.2260 0.6097 0.012630   1.2230 2.1620    3.5900  1001   4000
RR[9]  0.9662 0.3192 0.005141   0.4570 0.9251    1.7050  1001   4000
RR[10] 0.7817 0.2662 0.004268   0.3584 0.7484    1.3850  1001   4000
RR[11] 0.7007 0.2304 0.004228   0.3317 0.6711    1.2260  1001   4000
RR[12] 1.3510 0.3861 0.006772   0.7165 1.3100    2.2540  1001   4000
RR[13] 1.5120 0.5524 0.009909   0.6730 1.4340    2.8100  1001   4000
RR[14] 3.1230 1.1810 0.030370   1.3740 2.9390    5.9480  1001   4000
RR[15] 1.4980 0.4633 0.008328   0.7570 1.4390    2.5650  1001   4000
RR[16] 0.8205 0.2727 0.004188   0.3815 0.7903    1.4580  1001   4000
RR[17] 1.1660 0.3386 0.006009   0.6054 1.1320    1.9280  1001   4000
RR[18] 1.0840 0.3464 0.006399   0.5181 1.0400    1.8780  1001   4000
etc.....

基本上,我只想保留输出的列表结构。

2 个答案:

答案 0 :(得分:3)

选项1: 您可以保存整个工作区(例如,保存当前在R中的所有变量)。你这样做:

save.image("<file_name>.RData")

然后,当您重新打开R时,您将运行:

load("<file_name>.RData")

或者,在RStudio中,转到会话,您将看到保存和加载工作目录的选项。

如果你只想保存一个对象,你可以这样做:

选项2 :使用save(<R object>, file="<file_name>.RData")保存单个对象,然后使用load("<file_name>.RData")重新加载。

您还可以使用saveRDS(<R object>, file="<file_name>.RData")readRDS("<file_name>.RData")。与saveload相比,此方法允许您在将变量读回R后重命名该变量。

答案 1 :(得分:3)

使用RDS格式。

saveRDS(yourList, "yourFile.RDS")

我建议不要使用.Rdata,因为它已经过折旧且难以操作。 RDS可用于快速保存/加载大型对象。为此,您必须使用compress参数。

saveRDS(yourList, "yourFile.RDS", compress = FALSE)
yourList <- readRDS("yourFile.RDS")