[R] Embedding lists in matrices and matrices in lists
Michael Kogan
michael.kogan at gmx.net
Thu Aug 20 12:48:32 CEST 2009
Thanks, I was already told this solution by somebody (he just forgot to
add the mailing list as CC). Well, the purpose of the whole thing is to
get something like this:
http://home.att.net/~numericana/data/polycount.htm where the numbers in
the table cells give the number of matrices saved in the corresponding
cell. The program looks like this now:
http://paste.ubuntuusers.de/396117/ It even runs (though errors occur if
I add th process r into the main loop, but I'm still not done with it...).
Petr PIKAL schrieb:
> Hi
>
>
> No. It is the problem of scoping. AFAIK functions create their own
> environment and everything what is assigned inside the function does not
> affect outside objects unless you use <<- assignment operator.
>
> i<-100
> ff<-function(x) i<-x
> ff(10)
> i
> [1] 100
> fff<-function(x) i<<-x
> fff(200)
> i
> [1] 200
>
> However I would distract you from this practice. Personally I used such
> assignment only when I started with S/R about 10 years ago.
>
> Why do you want a database to be a matrix? It seems to be that list
> structure is in that case more flexible and there are *apply functions for
> manipulation with lists. E.g. structure lm(...) results in list and
> summary(lm(...)) is again a list with quite complicated structure.
>
> If I understand it correctly, during your computation you will have as a
> result matrices with arbitrary dimensions.
>
> I would make a list
>
> lll<-vector("list", 1)
>
> and in simple loop
>
> for (i in 1:n) {
>
> do any computation here together with sophisticated and complicated
> functions and assign results to your list
>
> lll[[1]] <- result of functions which itself can have quite complicated
> structure
>
> }
>
> If you want nested list just add another cycle and use
>
> lll[[i]][[j]] <-some other result.
>
> With choosing appropriate structure of your date you can save yourself
> much problems.
> Regards
> Petr
>
More information about the R-help
mailing list