Hi all, Would you generally consider NULL to be a vector? Base R functions are a little inconsistent: ## In favour ``` r identical(as.vector(NULL), NULL) #> [1] TRUE identical(as(NULL, "vector"), NULL) #> [1] TRUE # supports key vector vector generics length(NULL) #> [1] 0 NULL[c(3, 4, 5)] #> NULL NULL[[1]] #> NULL ``` ## Against ``` r is.vector(NULL) #> [1] FALSE is(NULL, "vector") #> [1] FALSE ``` ## Abstentions ``` r is.atomic(NULL) #> [1] TRUE # documentation states "returns NULL if x is of an atomic type (or NULL)" # is "or" exclusive or inclusive? ``` Hadley -- http://hadley.nz
On 23/07/2018 3:03 PM, Hadley Wickham wrote:> Hi all, > > Would you generally consider NULL to be a vector?According to the language definition (in the doc directory), it is not: "Vectors can be thought of as contiguous cells containing data. Cells are accessed through indexing operations such as x[5]. More details are given in Indexing. R has six basic (?atomic?) vector types: logical, integer, real, complex, string (or character) and raw. The modes and storage modes for the different vector types are listed in the following table." and later "There is a special object called NULL. It is used whenever there is a need to indicate or specify that an object is absent. It should not be confused with a vector or list of zero length." Duncan Murdoch Base R functions are> a little inconsistent: > > ## In favour > > ``` r > identical(as.vector(NULL), NULL) > #> [1] TRUE > > identical(as(NULL, "vector"), NULL) > #> [1] TRUE > > # supports key vector vector generics > length(NULL) > #> [1] 0 > NULL[c(3, 4, 5)] > #> NULL > NULL[[1]] > #> NULL > ``` > > ## Against > > ``` r > is.vector(NULL) > #> [1] FALSE > > is(NULL, "vector") > #> [1] FALSE > ``` > > ## Abstentions > > ``` r > is.atomic(NULL) > #> [1] TRUE > # documentation states "returns NULL if x is of an atomic type (or NULL)" > # is "or" exclusive or inclusive? > ``` > > Hadley >
On Mon, Jul 23, 2018 at 2:17 PM, Duncan Murdoch <murdoch.duncan at gmail.com> wrote:> On 23/07/2018 3:03 PM, Hadley Wickham wrote: >> >> Hi all, >> >> Would you generally consider NULL to be a vector? > > > According to the language definition (in the doc directory), it is not: > "Vectors can be thought of as contiguous cells containing data. Cells are > accessed through indexing operations such as x[5]. More details are given in > Indexing. > > R has six basic (?atomic?) vector types: logical, integer, real, complex, > string (or character) and raw. The modes and storage modes for the different > vector types are listed in the following table." > > and later > > "There is a special object called NULL. It is used whenever there is a need > to indicate or specify that an object is absent. It should not be confused > with a vector or list of zero length."Perfect, thanks! Also available online at https://cran.r-project.org/doc/manuals/r-release/R-lang.html#Vector-objects Hadley -- http://hadley.nz