[Rd] vapply confusion

Charles Berry ccberry at ucsd.edu
Sat Apr 12 18:10:10 CEST 2014


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



More information about the R-devel mailing list