How to save a list in a file and read it again (in R)?

I evaluated a model with a poLCA package in R and want to save the initial values ​​in a file, so I can re-evaluate the exact same model at any time.

This is a list of initial 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 meanings:

 > 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 

At first I thought that I could 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 exactly the same list that I get from the model. I already looked at stackoverflow, but could not find the answer to my question. I do not mind if the file is .csv, .txt or if it is one file or many. Sorry if I can not offer more ideas or code for my question. I don’t know where to start. Thanks for the help!

+5
source share
2 answers
 save(starting.values, file="fname.RData") ?save 

Allows you to save one or more R objects in one file.

Download

 load("fname.RData") 
+7
source

I also suggest that you study the rlist package and the proposed list.save function, as it provides additional flexibility when saving lists.

Example

As available in the 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

 - 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 can then load the list using the list.load function in the same package or you could read the lines from the exported yaml file. You may also consider looking at the list.select function and selecting objects from the list. Having said that, I believe that the easiest way to approach this is to simply save / load the complete list object.

+3
source

All Articles