Skip to content

assign() won't work

3 messages · Brian J. Lopes, Uwe Ligges, Peter Dalgaard

#
"Brian J. Lopes" wrote:
1) Please, tell your mailtool to wrap lines (and not to send in HTML
format which has been deleted anyway).

2) I don't get the point completely.

3) I guess you are going to use a list (of matrices or other appropriate
data structures) instead a huge number of single objects (in your case
matrices). Access to list elements should be much easier to handle.

Uwe Ligges
#
"Brian J. Lopes" <blopes at email.unc.edu> writes:
(please wrap your lines...)

Every time someone asks the question "How do I assign to x1,x2,x3,..."
they get the answer that assign(paste("x",i,sep=""),...) will work,
but they'd probably be better off using lists. I suspect you're in the
process of finding out why: The R language works much better on data
structures than on string representations. 

I'm not going to try and sort out the full details of your problem,
but if you had a list of arima() results, the solution could be as
simple as

sapply(olist,function(o)unlist(o[c("loglik","aic")]))

Here's a simple example, continuing example(arima):

data(lh)
l <- list(c(1,0,0),c(3,0,0),c(1,0,0))
olist <- lapply(l,arima, x=lh)
names(olist) <- sapply(l,deparse) 
sapply(olist,function(o)unlist(o[c("loglik","aic")]))

There are a couple of variations: (a) You might want to transpose the
resulting matrix using t(..) and (b) there's a simpler form

sapply(olist,"[",c("loglik","aic"))

but notice that (since the unlist() is gone) that gives you a list
matrix, rather than a matrix of numbers so the formatting is
different. (Run str() on the output to see what I mean.)