[R] Building a list

Joshua Wiley jwiley.psych at gmail.com
Mon May 31 00:00:46 CEST 2010

Hello Noah,

Does this work for you?

Chain <- vector("list", 10000)
for (j in 1:10000){
   coef <- c(1,2,3,4,5,6,7,8,9,10)  #would be actual MCMC samples
   Chain[[j]] <- rbind(Chain[[j]], coef)

If it does, this has the additional advantage that it tends to be
faster to initialize the list at size rather than expanding it as



On Sun, May 30, 2010 at 2:52 PM, Noah Silverman <noah at smartmediacorp.com> wrote:
> Hello,
> I need to build a "list of lists"
> We have 20 groups we are generating MCMC samples for.  There are 10
> coefficients, and 10000 MCMC iterations.
> I would like to store each iteration by-group in a list.  My problem is
> with the first iteration.
> Here is a toy example:
> Chain <- list()
> for (j in 1:10000){
>    coef <- c(1,2,3,4,5,6,7,8,9,10)  #would be actual MCMC samples
>    Chain[[j]] <- rbind(Chain[[j]], coef)
> }
> This returns an error, UNLESS I  initialize the first row of Chain[[j]]
> with something.
> The idea is that for any group, I can quickly extract, plot, average,
> etc the values for each coefficient.
> for example:
> Chain[[5]][,3] will give me all 10,000 values of coefficient 3 for group
> 5.
> Again, this seems to work, but I can't initialize the chain with a
> random value as it will cause problems with the data summary later.
> (Each row in Chain[[j]] will be out of sync by 1, subsequently all
> summary and plotting work will have to account for this - it can get
> messy in a large program.)
> Is there an easier way to do this?  Am I missing something?
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

Joshua Wiley
Senior in Psychology
University of California, Riverside

More information about the R-help mailing list