[R] Trouble with [sv]apply

Bert Gunter gunter.berton at gene.com
Fri Apr 20 15:29:04 CEST 2012


Worik:

On Fri, Apr 20, 2012 at 1:22 AM, Worik R <worikr at gmail.com> wrote:
> 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.

It certainly is -- where it should be:
?matrix  says:

"is.matrix returns TRUE if x is a vector and has a "dim" attribute of
length 2) and FALSE otherwise."

and even

"If you just want to convert a vector to a matrix, something like

  dim(x) <- c(nx, ny)
  dimnames(x) <- list(row_names, col_names)

will avoid duplicating x. "

Please do not fault R for your errors.

-- Bert
>
> 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.



-- 

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



More information about the R-help mailing list