which() does not handle NAs in named vectors. (PR#226)

ripley@stats.ox.ac.uk ripley@stats.ox.ac.uk
Thu, 15 Jul 1999 09:14:46 +0200 (MET DST)


Version:
 platform = sparc-sun-solaris2.6
 arch = sparc
 os = solaris2.6
 system = sparc, solaris2.6
 status = 
 status.rev = 0
 major = 0
 minor = 64.2
 year = 1999
 month = July
 day = 3
 language = R

-- It is unclear to me that the handling of NAs is desirable, and it
   has problems with names:

> z <- c(T,T,NA,F,T)
> names(z) <- letters[1:5]
> which(z)
Error: names attribute must be the same length as the vector
      
(Why do the vector and its names have different subscripts?  And while
you are correcting this,

Arguments:

       x: a logical vector or array.  `NA's are allowed an
          omitted.
  
has a typo, and the logic can be simplified: see below.)

On Thu, 15 Jul 1999, Martin Maechler wrote:

> >>>>> "BDR" == Prof Brian D Ripley <ripley@stats.ox.ac.uk> writes:
> 
>     BDR> On Wed, 14 Jul 1999, Friedrich Leisch wrote:
>     >> >>>>> On Wed, 14 Jul 1999 04:09:21, >>>>> Peter B Mandeville (PBM)
>     >> wrote:
>     >> 
>     PBM> I have a vector Pes with 600 elements some of which are NA's. How
>     PBM> can I form a vector of the indices of the NA's.
>     >>
>     PBM> for(i in 1:600) if(is.na(Pes[i])) print(i)
>     >>
>     PBM> prints the indices of the NA's but I can't figure out how to put
>     PBM> the results in a vector.
>     >>  try this:
>     >> 
>     >> x <- (1:length(Pes))[is.na(Pes)]
> 
>     BDR> Tip: that sort of thing often fails for a length 0 vector. The
>     BDR> `approved' spell is
> 
>     BDR> seq(along=Pes)[is.na(Pes)]
> 
>     BDR> In this case it does not matter as the subscript is of length 0,
>     BDR> but it has floored enough library/package writers to be worth
>     BDR> thinking about.
> 
> Good teaching about seq() vs.  1:n 
> 
> However, the solution I gave 
> 
>      which(is.na(Pes))
> 
> is the one I stilly really recommend; 
> it does deal with 0-length objects, and it keeps names when there are some,
> and it has an `arr.ind = FALSE' argument to return array indices instead of
> vector indices when so desired.

Yes, but

-- It is not in S (so causing difficulty in porting from R to S)
-- It looks a relatively expensive operation.
-- Internally which could be simplified by using seq(along=) as it
   is a wrapper for this construct, but actually the separate
   handling of n == 0 is unnecessary (as logic & !is.na(logic) will have
   length zero.)

Brian

-- 
Brian D. Ripley,                  ripley@stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272860 (secr)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595


-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-devel mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-devel-request@stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._