|
View:
New views
4 Messages
—
Rating Filter:
Alert me
|
|
|
which data structure to choose to keep multile objects?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? Thanks! Tim |
|
|
Re: which data structure to choose to keep multile objects?your could try something like this:
xprocess<-function(max_val) { result <- list() for (iter in 2: max_val) { zz = sprintf( "z%s", iter ) # use 'character' for indexing the list result[[as.character(iter)]] <-nnmf(X,iter) } result } xprocess(10) On Fri, Nov 6, 2009 at 6:58 PM, clue_less <suhai_tim_liu@...> 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? > > Thanks! > > Tim > > -- > View this message in context: http://old.nabble.com/which-data-structure-to-choose-to-keep-multile-objects--tp26231601p26231601.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help@... 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. > -- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem that you are trying to solve? ______________________________________________ R-help@... 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. |
|
|
Re: which data structure to choose to keep multile objects?On Fri, Nov 6, 2009 at 11:58 PM, clue_less <suhai_tim_liu@...> 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 ______________________________________________ R-help@... 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. |
|
|
Re: which data structure to choose to keep multile objects?If you use the replicate or lapply functions then this will be taken care of for you (a list will be created containing your function output).
-- Gregory (Greg) L. Snow Ph.D. Statistical Data Center Intermountain Healthcare greg.snow@... 801.408.8111 > -----Original Message----- > From: r-help-bounces@... [mailto:r-help-bounces@r- > project.org] On Behalf Of clue_less > Sent: Friday, November 06, 2009 4:58 PM > To: r-help@... > Subject: [R] which data structure to choose to keep multile objects? > > > 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? > > Thanks! > > Tim > > -- > View this message in context: http://old.nabble.com/which-data- > structure-to-choose-to-keep-multile-objects--tp26231601p26231601.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help@... 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. ______________________________________________ R-help@... 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. |
| Free embeddable forum powered by Nabble | Forum Help |