[R] why Vectorize conjures a list, not a vector?

Zhang Weiwu zhangweiwu at realss.com
Thu Aug 15 05:01:24 CEST 2013


The manual seems to suggest, with the SIMPLIFY = TRUE default option, 
Vectorize would conjure a vector if possible.

Quote:

     SIMPLIFY: logical or character string; attempt to reduce the result to
       a vector, matrix or higher dimensional array; see the
       ‘simplify’ argument of ‘sapply’.

I assume, if each run of the function results a vector of the same type, 
the result should be a vector as well; there is a need of list only when 
data are of different type.

Or, given vectors of the same type, conjure vectors of the same type.

But it doesn't work that way -- see below -- so what's the magic inside?

REPRODUCE:

First, to make sure each run of the function always return vector of the 
same time:

> for (datafile in list.files(full.names=TRUE,"16b")) print(mode(list.files(full.names=TRUE,datafile)))
[1] "character"
[1] "character"
[1] "character"
[1] "character"
[1] "character"
[1] "character"
[1] "character"
[1] "character"
[1] "character"
[1] "character"
[1] "character"


Then, vectorize it:

> datafiles <- c(Vectorize(list.files, "path")(full.names=TRUE,path = list.files(base_dir,full.names=TRUE)))
> mode(datafiles)
[1] "list"

The same happened with sapply, which should generate list only if a vector 
is impossible -- it generated a list when every result is a vector:

> mode(sapply(list.files(base_dir,full.names=TRUE), list.files))
[1] "list"


More information about the R-help mailing list