Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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

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

like image 481
SEMson Avatar asked Aug 08 '15 13:08

SEMson


People also ask

How do I save a value list in R?

You could simply use save(...) or save. image(...) to save your list object as rdata file. Then, you can download this file from your server and load it into the current R work space of your computer (function load(...)).

Can I save a list as a CSV in R?

You can use the sink() function to quickly export a list to a CSV file or text file in R.

What is Save () in R?

save writes an external representation of R objects to the specified file. The objects can be read back from the file at a later date by using the function load or attach (or data in some cases).


1 Answers

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

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

Load with

readRDS("fname.RData") 

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

like image 142
Whitebeard Avatar answered Oct 13 '22 01:10

Whitebeard