简体   繁体   中英

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

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.

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

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

Load with

load("fname.RData")

I would also suggest that you explore rlist package and the offered list.save function as it provides additional flexibility when saving lists.

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

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

For those who are getting the character of the name of the variables.

vect_to_save <- c(1,2,3)
save(vect_to_save, file="vec.RData")  # Till here everything is ok. 

# Now, see the difference
vect_to_save <- load(file="vec.RData")
print(vect_to_save)  # Prints "vect_to_save"

# To load you have to
load(file="vec.RData")
print(vect_to_save)  # Now, yes!
# [1] 1 2 3

Don't assign the output of load() to a var. It directly loads the var into workspace.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM