From ccberry at ucsd.edu  Sat Apr 12 18:10:10 2014
From: ccberry at ucsd.edu (Charles Berry)
Date: Sat, 12 Apr 2014 16:10:10 +0000
Subject: [Rd] vapply confusion
References: <CADUbQ5gKwR+gMpjALkizH77zNj6ds4NbxX_-Kq5OjO+gqJo4=g@mail.gmail.com>
Message-ID: <loom.20140412T180140-839@post.gmane.org>

Dominick Samperi <djsamperi <at> gmail.com> writes:

> 
> The following code seems to contain an inconsistency in
> the behavior of vapply(). Am I missing something here?
> 
> ## This function assumes v is a 3d vector, beta a scalar.
> f3d <- function(v,beta) { v+beta }
> 
> ## This expression applies f3d to a vector of scalars, and
> ## specifies the template 'array(10,3)' for the return value.
> dat <- vapply(seq(0,1,length=10), function(beta) {
>   f3d(c(0,0,0), beta) }, array(10,3))
> 

Same result as that of sapply(...

> dim(dat) # 3 10 (the dimensions are the reverse of the template?)
> 

No the template has dimension '3'

> dim(array(10,3))
[1] 3
> 

and X has length 10.

Editting the help page:

"If FUN.VALUE is [an array, the result is] an array a with dim(a) ==
c(dim(FUN.VALUE), length(X))."

which is what you got.

HTH,

Chuck



[Rd] vapply confusion
Charles Berry