[R] vector of NA indices

Prof Brian D Ripley ripley at stats.ox.ac.uk
Wed Jul 14 19:49:29 CEST 1999


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 can I
> PBM> 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 the
> PBM> results in a vector.
> 
> try this:
> 
> x <- (1:length(Pes))[is.na(Pes)]

Tip: that sort of thing often fails for a length 0 vector. The `approved'
spell is

seq(along=Pes)[is.na(Pes)]

In this case it does not matter as the subscript is of length 0, but it
has floored enough library/package writers to be worth thinking about.

Brian

-- 
Brian D. Ripley,                  ripley at 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-help 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-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list