# [R] R 0.64.2 indexing

Thomas Lumley thomas at biostat.washington.edu
Thu Aug 19 00:27:28 CEST 1999

```On Thu, 19 Aug 1999, Peppy wrote:

> Hi,
>
> > Say we have a large-sized vector of observation A and a time vector with
> > the same size.  The latter vector basically is the time flag when ith
> > obervation of A occurs, hence A[i] occurs at T[i].
> >
> > I have no trouble with doing: T[A <= 10 | A >=20] for example
> >
> > Now, what I am confused: I tried to pull out when the largest value of A
> > with:
> >
> > T[A == max(A, na.rm=TRUE)]
> >
> > this statement comes up with a number of NA's although I activate the
> > na.rm flag to TRUE.  the next thing I accidently type this:

The NAs match the NAs in A. Suppose that the largest value of A is 42
Then
T[A == max(A, na.rm=TRUE)]
is the same as
T[A == 42]

But A==42 is NA if A is NA, so this returns the maximum and a bunch of
NAs.

T[A ==max(A, na.rm=TRUE) & !is.na(A)]
does what you want

> >
> > T[A = max(A, na.rm=TRUE)]
> >
> > with single "=", it works!! I thought logical "equal" is double equals,
> > "==".

This is  much more subtle and interesting. It doesn't actually work, it
just looks as though it does.

T[absolutely.anything.you.want=42]
has the same effect as
T

This is because "["  doesn't check its argument names
T[A=42] is shorthand for  "["(T,A=42), and the A is ignored.

The same thing happens with some other primitive functions
> sqrt(a.long.parameter.name=4)
 2

This means that if the largest element of A is 42, T[A=max(A,na.rm=T)]
returns T, which is not at all what you want (except possibly by
coincidence)

Thomas Lumley
Assistant Professor, Biostatistics
University of Washington, Seattle

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

```