s-news
[Top] [All Lists]

Re: Vector of same values

To: Ray Brownrigg <ray@mcs.vuw.ac.nz>
Subject: Re: Vector of same values
From: <ripley@stats.ox.ac.uk>
Date: Wed, 30 Oct 2002 22:20:28 +0000 (GMT)
Cc: <pillindra@quantros.com>, <s-news@lists.biostat.wustl.edu>
In-reply-to: <200210302057.g9UKvUt3028864@tahi.mcs.vuw.ac.nz>
On Thu, 31 Oct 2002, Ray Brownrigg wrote:

> > x <- c(10,10,10,10,10)
> > should return "TRUE"
> >
> > y <- c(10,20,5,10,10)
> > should return "FALSE".
> >
> > Is there any command in s-plus that I can use to find out this.
> > I am trying to use which(x) and match command, but I couldn't work around
> > with those functions to accomplish this.
> >
> Try:
> all(x==x[1])

That's a good starting point, but think about missing values (if they
might occur).  Something like

all(!is.na(x)) && all(x==x[1])

looks about right: this is true only if all values are known and are the
same.

Another way to do this (S+6.1 only) is all(x[1] %in% x) since the inner
expression is never NA.

I've found it very helpful to always consider missing values and so help
avoid surprises.  S has a three-valued logic (T,F,NA) unlike e.g. C.

-- 
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


<Prev in Thread] Current Thread [Next in Thread>