[R] Summary: vectorizing test for equality

John Miyamoto jmiyamot at u.washington.edu
Fri Dec 20 12:54:11 CET 2002


Thanks for many prompt and useful comments.  The question was:  if x is
the vector

> x <- c( 2, NA, 1, 5, 3)
> x
[1]  2 NA  1  5  3

Then x == 5 yields

> x == 5
[1] FALSE    NA FALSE  TRUE FALSE

whereas what I want is

FALSE  FALSE  FALSE  TRUE  FALSE
#--------------
Matthew Dowle sent a simple solution:

> x
[1]  2 NA  1  5  3
> x==5 & !is.na(x)
[1] FALSE FALSE FALSE  TRUE FALSE

Thanks also to Brian Ripley who suggested that I look at chapter 2 of
MASS4, and Eric Lecoutre who pointed out that the 'which' function could
be useful for this problem.

John Miyamoto


--------------------------------------------------------------------
John Miyamoto, Dept. of Psychology, Box 351525
University of Washington, Seattle, WA 98195-1525
Phone 206-543-0805, Fax 206-685-3157, Email jmiyamot at u.washington.edu
Homepage http://faculty.washington.edu/jmiyamot/
--------------------------------------------------------------------




More information about the R-help mailing list