[R] Save a list of list and search for values
Duncan Murdoch
murdoch.duncan at gmail.com
Thu Feb 26 15:39:12 CET 2015
On 26/02/2015 9:27 AM, Alaios via R-help wrote:
> Dear all,in my code I am using the mix() function that returns results in a list. The result looks like
> List of 10
> $ parameters :'data.frame': 2 obs. of 3 variables:
> ..$ pi : num [1:2] 0.77 0.23
> ..$ mu : num [1:2] -7034 162783
> ..$ sigma: num [1:2] 20235 95261
> $ se :'data.frame': 2 obs. of 3 variables:
> ..$ pi.se : num [1:2] 0.0423 0.0423
> ..$ mu.se : num [1:2] 177 12422
> ..$ sigma.se: num [1:2] 1067 65551
> $ distribution: chr "norm"
> $ constraint :List of 8
> ..$ conpi : chr "NONE"
> ..$ conmu : chr "NONE"
> ..$ consigma: chr "NONE"
> ..$ fixpi : NULL
> ..$ fixmu : NULL
> ..$ fixsigma: NULL
> ..$ cov : NULL
> ..$ size : NULL
> $ chisq : num 28
> $ df : num 5
> $ P : num 3.67e-05
> $ vmat : num [1:5, 1:5] 1.79e-03 -3.69e-01 -1.17e+02 2.95e+01 -2.63e+03 ...
> $ mixdata :Classes ‘mixdata’ and 'data.frame': 11 obs. of 2 variables:
> ..$ X : num [1:11] 1e+04 2e+04 3e+04 4e+04 5e+04 6e+04 7e+04 8e+04 9e+04 1e+05 ...
> ..$ count: int [1:11] 993 137 82 30 21 5 7 14 21 2 ...
> $ usecondit : logi FALSE
> - attr(*, "class")= chr "mix"
>
> In my code I am trying around 10.000 fit (and each of these fits returns the list above) and I want to keep those in a way that later on I would be able to search inside all the lists.For example I would like to find inside those 10.000 lists which one has the smallest $chisq value. What would be a suitable way to implement that in R? Luckily I am working in a computer with a lot of ram so storing 10.000 lists temporary in memory before saving to disk would not be a problem.
> What would you suggest me?
If all of the lists have the same components, then it would be
convenient to convert them into a big matrix or dataframe, with one row
per fit. It would need to be a dataframe if you include character data
along with the numbers, but a matrix would be faster, if it's only
numbers that you need. You'd use code like this to produce the matrix:
results <- matrix(NA_real_, 10000, ncols = .... however many you keep ....)
for (i in 1:10000) {
fit <- .... code to get the fit object ....
results[i,] <- with(fit, c(parameters$pi, parameters$mu,
parameters$sigma, ...... fill in the rest ......)
}
Duncan Murdoch
More information about the R-help
mailing list