[R] Trouble with [sv]apply
Petr PIKAL
petr.pikal at precheza.cz
Fri Apr 20 12:31:55 CEST 2012
Hi
> On Fri, Apr 20, 2012 at 4:42 PM, Jeff Newmiller
> <jdnewmil at dcn.davis.ca.us> wrote:
> > 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.
>
> Thank you. That was helpful. I did read the help on [vsl]apply. But
> the idea that matrices are "folded vectors" was, of course not there.
Here is Details section of help page for array
An array in R can have one, two or more dimensions. It is simply a vector
which is stored with additional attributes giving the dimensions
(attribute "dim") and optionally names for those dimensions (attribute
"dimnames").
and chapter 2.8 of R - intro
2.8 Other types of objects
Vectors are the most important type of object in R, but there are several
others which we will
meet more formally in later sections.
matrices or more generally arrays are multi-dimensional generalizations
of vectors. In fact,
they are vectors that can be indexed by two or more indices and will be
printed in special
ways. See Chapter 5 [Arrays and matrices], page 18.
Quite short and usefull chapter.
Regards
Petr
>
> Worik
>
> >
---------------------------------------------------------------------------
> > 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.
> >
>
> ______________________________________________
> 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.
More information about the R-help
mailing list