Friends I clearly donot understand how sapply and vapply work. What I have is a function that returns a matrix with an indeterminate number of rows (some times zero) but a constant number of columns. I cannot reliably use an apply function to assemble the matrices into a matrix. I am not sure it is possible. I can demonstrate the core of my confusion with this simple code. A.f <- function(i){ ret <- matrix("a", i, 7) cat(i, class(ret), dim(ret), "\n") return(ret) } V.f <- function(){ SS <- vapply(c(1,2), A.f, rep('a', 7)) return(SS) } S.f <- function(){ SS <- sapply(c(1,2), A.f) cat("SS", class(SS), dim(SS), "\n") return(SS) } Calling V.f() fails:> V.f()1 matrix 1 7 2 matrix 2 7 Error in vapply(c(1, 2), A.f, rep("a", 7)) : values must be length 7, but FUN(X[[2]]) result is length 14>Calling S.f() returns a list. Do I have to accept I am going to be getting a list and I have to assemble a matrix in a loop? cheers Worik
If you read the help, it talks about compiling vectors into matrices, or scalars into vectors. It does not say anything about combining matrices. For the error about 14 elements, you should keep in mind that matrices are just vectors with dim attributes that indicate how the linear memory is to be "folded". As far as I know, the standard way to handle combining matrices as you want to would involve storing them in a list and using Reduce and rbind. If you can vectorize the whole process instead of segmenting it by groups of rows then you can speed things up considerably. --------------------------------------------------------------------------- Jeff Newmiller The ..... ..... Go Live... DCN:<jdnewmil at dcn.davis.ca.us> Basics: ##.#. ##.#. Live Go... Live: OO#.. Dead: OO#.. Playing Research Engineer (Solar/Batteries O.O#. #.O#. with /Software/Embedded Controllers) .OO#. .OO#. rocks...1k --------------------------------------------------------------------------- Sent from my phone. Please excuse my brevity. Worik R <worikr at gmail.com> wrote:>Friends > >I clearly donot understand how sapply and vapply work. > >What I have is a function that returns a matrix with an indeterminate >number of rows (some times zero) but a constant number of columns. I >cannot reliably use an apply function to assemble the matrices into a >matrix. I am not sure it is possible. > >I can demonstrate the core of my confusion with this simple code. > >A.f <- function(i){ > ret <- matrix("a", i, 7) > cat(i, class(ret), dim(ret), "\n") > return(ret) >} >V.f <- function(){ > SS <- vapply(c(1,2), > A.f, > rep('a', 7)) > return(SS) >} >S.f <- function(){ > SS <- sapply(c(1,2), > A.f) > cat("SS", class(SS), dim(SS), "\n") > return(SS) >} > > >Calling V.f() fails: > >> V.f() >1 matrix 1 7 >2 matrix 2 7 >Error in vapply(c(1, 2), A.f, rep("a", 7)) : > values must be length 7, > but FUN(X[[2]]) result is length 14 >> > > >Calling S.f() returns a list. > > >Do I have to accept I am going to be getting a list and I have to >assemble a matrix in a loop? > >cheers >Worik > >______________________________________________ >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.
?? Maybe: do.call(rbind, list.of.your;matrices) ?do.call (if you can't do it with explicit loops (i.e. for(), while()), apply type functions are probably not what you want). -- Bert On Thu, Apr 19, 2012 at 2:28 PM, Worik R <worikr at gmail.com> wrote:> Friends > > I clearly donot understand how sapply and vapply work. > > What I have is a function that returns a matrix with an indeterminate > number of rows (some times zero) but a constant number of columns. ?I > cannot reliably use an apply function to assemble the matrices into a > matrix. ?I am not sure it is possible. > > I can demonstrate the core of my confusion with this simple code. > > A.f <- function(i){ > ?ret <- matrix("a", i, 7) > ?cat(i, class(ret), dim(ret), "\n") > ?return(ret) > } > V.f <- function(){ > ?SS <- vapply(c(1,2), > ? ? ? ? ? ? ? ? A.f, > ? ? ? ? ? ? ? ? rep('a', 7)) > ?return(SS) > } > S.f <- function(){ > ?SS <- sapply(c(1,2), > ? ? ? ? ? ? ? ? A.f) > ?cat("SS", class(SS), dim(SS), "\n") > ?return(SS) > } > > > Calling V.f() fails: > >> V.f() > 1 matrix 1 7 > 2 matrix 2 7 > Error in vapply(c(1, 2), A.f, rep("a", 7)) : > ?values must be length 7, > ?but FUN(X[[2]]) result is length 14 >> > > > Calling S.f() returns a list. > > > Do I have to accept I am going to be getting a list and I have to > assemble a matrix in a loop? > > cheers > Worik > > ______________________________________________ > 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.-- Bert Gunter Genentech Nonclinical Biostatistics Internal Contact Info: Phone: 467-7374 Website: http://pharmadevelopment.roche.com/index/pdb/pdb-functional-groups/pdb-biostatistics/pdb-ncb-home.htm