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

Hervé Pagès hpages at fhcrc.org
Thu Aug 15 07:57:46 CEST 2013


Hi Zhang,

First note that a list is a vector (try is.vector(list())).
The documentation for sapply() and Vectorize() should say *atomic*
vector instead of vector in the desccription of the 'simplify' and
'SIMPLIFY' arguments.

So in order for sapply() to be able to simplify the result, all runs
of the function not only need to produce an atomic vector of the same
type, but also of the same length. If this common length is 1, then the
final result can be simplified to an atomic vector of the same length
as the input.

HTH,

H.


On 08/14/2013 08:01 PM, Zhang Weiwu wrote:
>
> 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"
>
>
> ______________________________________________
> 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.
>

-- 
Hervé Pagès

Program in Computational Biology
Division of Public Health Sciences
Fred Hutchinson Cancer Research Center
1100 Fairview Ave. N, M1-B514
P.O. Box 19024
Seattle, WA 98109-1024

E-mail: hpages at fhcrc.org
Phone:  (206) 667-5791
Fax:    (206) 667-1319



More information about the R-help mailing list