On Oct 17, 2010, at 3:56 PM, William Dunlap wrote:
I had been thinking of:
x <- c(1, (2^(0.5))^2 , 3, 5, (2^(0.5))^2 , 3, 1)
y <- 2
x[-which(zapsmall(x-y) == 0)]
[1] 1 3 5 3 1
Using which() to convert logicals into integer
subscripts is almost always unnecessary and often wrong.
At one time I believed that too. However, in the situation where the
test produces NA rather than a numeric value when one is indexing in
the first argument. I have had the unpleasant experience of pages if
useless and frustrating to understand output because of this "feature".
I learned to either use which() in the first argument to "[" or to use
subset to avoid inadvertent "returns" from logical indexing.
> x <- 1:10
> y <- log(x-5)
Warning message:
In log(x - 5) : NaNs produced
> x[y>-Inf]
[1] NA NA NA NA 6 7 8 9 10
> x[which(y>-Inf)]
[1] 6 7 8 9 10
If that test were used in a dataframe indexing, the entire line might
come back as a "result".
In this case it fails when no x is close to y,
because integer(0) is the same thing as -integer(0):
x[-which(zapsmall(x-10) == 0)]
numeric(0)
The whichless version, using logical subscripts,
works (in this case we want all of x):
x[zapsmall(x-10)!=0]
[1] 1 2 3 5 2 3 1
Maybe the rule should be don't use the -which construction:
> x <- c(1, (2^(0.5))^2 , 3, 5, (2^(0.5))^2 , 3, 1)
> y <- 2
> x[which(zapsmall(x-10) != 0)]
[1] 1 2 3 5 2 3 1
--
David.
When using logicals as subscripts, read the "["
as "such that".
Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com
______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.