[R] which data structure to choose to keep multile objects?
Barry Rowlingson
b.rowlingson at lancaster.ac.uk
Sat Nov 7 14:32:32 CET 2009
On Fri, Nov 6, 2009 at 11:58 PM, clue_less <suhai_tim_liu at yahoo.com> wrote:
>
> I have a function called nnmf which takes in one matrix and returns two
> matrices. for example,
>
>> X
> [,1] [,2] [,3] [,4]
> [1,] 1 4 7 10
> [2,] 2 5 8 11
> [3,] 3 6 9 12
>
>> z=nnmf(X,2)
>
>> z$W
> [,1] [,2]
> [1,] 0.8645422 0.6643681
> [2,] 1.7411863 0.5377504
> [3,] 2.6179287 0.4111063
>> z$H
> [,1] [,2] [,3] [,4]
> [1,] 1.14299486 1.692260 2.241279 2.79030
> [2,] 0.01838514 3.818559 7.619719 11.42087
>
> ----------------------------------------
>
> Now I would like to run it many times --
>
> z2 = nnmf(X,2)
> z3 = nnmf(X,3)
> z4 = nnmf(X,4)
> z5 = nnmf(X,5)
> ...
>
> But I would like to do it automatically , something like -
>
> xprocess<-function(max_val) {
> for (iter in 2: max_val) {
>
> zz = sprintf( "z%s", iter )
>
> zz <-nnmf(X,iter)
>
> }
>
> }
>
> xprocess(10)
>
>
> ----
>
> But how could I keep collection of my results each run?
>
> Shall I have a data structure to keep appending results?
>
> something like
>
> theta = {}
>
> ?
>
> which data structure to choose to keep multile objects?
>
You're already using one! It's called a list:
zz=list()
for(i in 1:10){
zz[[i]] = nnmf(X,i)
}
then you can do:
zz[[1]]$W and zz[[1]]$H
Note the BIG difference between zz[1] and zz[[1]] though.
Barry
More information about the R-help
mailing list