clue_less
2009-Nov-06 23:58 UTC
[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.
jim holtman
2009-Nov-07 12:59 UTC
[R] 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 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? > > 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 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. >-- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem that you are trying to solve?
Barry Rowlingson
2009-Nov-07 13:32 UTC
[R] which data structure to choose to keep multile objects?
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
Greg Snow
2009-Nov-08 04:35 UTC
[R] 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 at imail.org 801.408.8111> -----Original Message----- > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r- > project.org] On Behalf Of clue_less > Sent: Friday, November 06, 2009 4:58 PM > To: r-help at r-project.org > 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 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.