[Rd] extract function "[" and empty index

Bengoechea Bartolomé Enrique (SIES 73) enrique.bengoechea at credit-suisse.com
Mon Mar 10 13:08:52 CET 2008


Beware x[TRUE] returns the same as x[] only if x is NOT a zero-length vector (at least until R 2.5.1):

> numeric(0)[]
numeric(0)

> numeric(0)[TRUE]
[1] NA


Enrique

> -----Original Message-----
> ------------------------------
> 
> Message: 7
> Date: Mon, 10 Mar 2008 02:29:32 +0800
> From: "Laurent Gautier" <lgautier at gmail.com>
> Subject: Re: [Rd] extract function "[" and empty index
> To: "Gabor Grothendieck" <ggrothendieck at gmail.com>
> Cc: r-devel at r-project.org
> Message-ID:
> 	<27d1e6020803091129h19bbf3ffu1265ecd04b35993 at mail.gmail.com>
> Content-Type: text/plain; charset=ISO-8859-1
> 
> Thanks, I was forgetting the recycling rule.
> 
> 
> L.
> 
> 
> 2008/3/9, Gabor Grothendieck <ggrothendieck at gmail.com>:
> > Use TRUE.
> >
> >
> >  On Sun, Mar 9, 2008 at 5:05 AM, Laurent Gautier 
> <lgautier at gmail.com> wrote:
> >  > Dear list,
> >  >
> >  > I am having a question regarding the extract function "[".
> >  >
> >  > The man page says that one usage with k-dimensional arrays is to
> >  > specify k indices to "[", with an empty index indicating that all
> >  > entries in that dimension are selected.
> >  >
> >  > The question is the following: is there an R object 
> qualifying as an
> >  > "empty index" ? I understand that the lazy evaluation of 
> parameters
> >  > allows one to
> >  > have genuinely missing parameters, but I would like to 
> have an object
> >  > instead. I understand that one can always have an 
> if/else workaround,
> >  > but I thought should ask, just in case.
> >  >
> >  > I tried with NULL but with little success, as it appears 
> to give the
> >  > same results
> >  > as an empty vector.
> >  > > m = matrix(1, 2, 2)
> >  > > m[1, NULL]
> >  > numeric(0)
> >  > > m[1, integer(0)]
> >  > numeric(0)
> >  >
> >  > Since I was at it, I noted that the result obtained with 
> "numeric(0)"
> >  > definitely makes sense but could as well be seen as 
> challenging the
> >  > concept of an empty index presented in the man page (One 
> could somehow
> >  > expect the presence of an object  meaning "everything" 
> rather than
> >  > "missingness" meaning it).
> >  >
> >  >
> >  > Thanks,
> >  >
> >  >
> >  > Laurent
> >  >
> >
> > > ______________________________________________
> >  > R-devel at r-project.org mailing list
> >  > https://stat.ethz.ch/mailman/listinfo/r-devel
> >  >
> >
> 
> 
> -- 



More information about the R-devel mailing list