[R] Why does a[which(b == c[d])] not work?

2003-10-08 Thread Thomas Bock
Dear list,

I can not understand why the expression in
the subject does not work correct:
 dcrn[which(fn == inve[2])]
numeric(0)
 inve[2]
[1] 406.7
 dcrn[which(fn == 406.7)]
[1] 1.3994e-07 1.3988e-07 1.3953e-07 1.3966e-07 1.3953e-07 1.3968e-07
Is this a kick self problem or an bug?

Thaks very much
Thomas
__
[EMAIL PROTECTED] mailing list
https://www.stat.math.ethz.ch/mailman/listinfo/r-help


Re: [R] Why does a[which(b == c[d])] not work?

2003-10-08 Thread Achim Zeileis
On Wednesday 08 October 2003 11:27, Thomas Bock wrote:

 Dear list,

 I can not understand why the expression in

 the subject does not work correct:
   dcrn[which(fn == inve[2])]

 numeric(0)

   inve[2]

 [1] 406.7

   dcrn[which(fn == 406.7)]

  [1] 1.3994e-07 1.3988e-07 1.3953e-07 1.3966e-07 1.3953e-07
 1.3968e-07

The reason is that you shouldn't compare numeric output like that, 
consider the following example:

R x - 406.7 + 1e-5 
R x
[1] 406.7
R x == 406.7
[1] FALSE

whereas

R x - 406.7 + 1e-20 
R x
[1] 406.7
R x == 406.7
[1] TRUE

that is
  1.) `==' comparisons have a certain tolerance
  2.) the print output is not necessarily precisely your number

Instead of using `==' you should use a comparison with a certain 
tolerance you can specify...

hth,
Z


 Is this a kick self problem or an bug?

 Thaks very much
 Thomas

 __
 [EMAIL PROTECTED] mailing list
 https://www.stat.math.ethz.ch/mailman/listinfo/r-help

__
[EMAIL PROTECTED] mailing list
https://www.stat.math.ethz.ch/mailman/listinfo/r-help


Re: [R] Why does a[which(b == c[d])] not work?

2003-10-08 Thread Peter Dalgaard BSA
Thomas Bock [EMAIL PROTECTED] writes:

 Dear list,
 
 I can not understand why the expression in
 the subject does not work correct:
 
   dcrn[which(fn == inve[2])]
 numeric(0)
   inve[2]
 [1] 406.7
   dcrn[which(fn == 406.7)]
  [1] 1.3994e-07 1.3988e-07 1.3953e-07 1.3966e-07 1.3953e-07 1.3968e-07
 
 Is this a kick self problem or an bug?

Kick self, most likely. What is 

inve[2] - 406.7

-- 
   O__   Peter Dalgaard Blegdamsvej 3  
  c/ /'_ --- Dept. of Biostatistics 2200 Cph. N   
 (*) \(*) -- University of Copenhagen   Denmark  Ph: (+45) 35327918
~~ - ([EMAIL PROTECTED]) FAX: (+45) 35327907

__
[EMAIL PROTECTED] mailing list
https://www.stat.math.ethz.ch/mailman/listinfo/r-help


Re: [R] Why does a[which(b == c[d])] not work?

2003-10-08 Thread Martin Maechler
Your question has been answered by Achim and Peter Dalgaard (at least).

Just a note:

Using  
   a[which(logic)] 
looks like a clumsy and inefficient way of writing
   a[ logic ]

and I think you shouldn't propagate its use ...

Martin Maechler [EMAIL PROTECTED] http://stat.ethz.ch/~maechler/
Seminar fuer Statistik, ETH-Zentrum  LEO C16Leonhardstr. 27
ETH (Federal Inst. Technology)  8092 Zurich SWITZERLAND
phone: x-41-1-632-3408  fax: ...-1228   

__
[EMAIL PROTECTED] mailing list
https://www.stat.math.ethz.ch/mailman/listinfo/r-help


Re: [R] Why does a[which(b == c[d])] not work?

2003-10-08 Thread Tony Plate
At Wednesday 03:06 PM 10/8/2003 +0200, Martin Maechler wrote:
Your question has been answered by Achim and Peter Dalgaard (at least).

Just a note:

Using
   a[which(logic)]
looks like a clumsy and inefficient way of writing
   a[ logic ]
and I think you shouldn't propagate its use ...
What then is the recommended way of treating an NA in the logical subset as 
a FALSE? (Or were you just talking about the given example, which didn't 
have this issue.  However, you admonition seemed more general.)

As in:
 x - 1:4
 y - c(1,2,NA,4)
 x[y %% 2 == 0]
[1]  2 NA  4
 x[which(y %% 2 == 0)]
[1] 2 4

Sometimes one might want the first result, but more usually, I want the 
second, and using which() seems a convenient way to get it.

-- Tony Plate

__
[EMAIL PROTECTED] mailing list
https://www.stat.math.ethz.ch/mailman/listinfo/r-help


Re: [R] Why does a[which(b == c[d])] not work?

2003-10-08 Thread Gabor Grothendieck

Here are some different ways of doing this. Don't know whether any
could be considered superior to the others.

# y[x==5] regarding NAs in x as not matching
x - c(5, NA, 7, 5, NA, 3)
y - c(1,  2, 3, 4,  5, 6)
subset(y,x==5)
y[x %in% 5]
y[x %in% c(5)]
y[which(x==5)]


--- 
Date: Wed, 08 Oct 2003 08:58:32 -0600 
From: Tony Plate [EMAIL PROTECTED]
 
At Wednesday 03:06 PM 10/8/2003 +0200, Martin Maechler wrote:
Your question has been answered by Achim and Peter Dalgaard (at least).

Just a note:

Using
 a[which(logic)]
looks like a clumsy and inefficient way of writing
 a[ logic ]

and I think you shouldn't propagate its use ...

What then is the recommended way of treating an NA in the logical subset as 
a FALSE? (Or were you just talking about the given example, which didn't 
have this issue. However, you admonition seemed more general.)

As in:
 x - 1:4
 y - c(1,2,NA,4)
 x[y %% 2 == 0]
[1] 2 NA 4
 x[which(y %% 2 == 0)]
[1] 2 4


Sometimes one might want the first result, but more usually, I want the 
second, and using which() seems a convenient way to get it.

-- Tony Plate


___
No banners. No pop-ups. No kidding.
Introducing My Way - http://www.myway.com

__
[EMAIL PROTECTED] mailing list
https://www.stat.math.ethz.ch/mailman/listinfo/r-help


Re: [R] Why does a[which(b == c[d])] not work?

2003-10-08 Thread Jason Turner
Achim Zeileis wrote:

On Wednesday 08 October 2003 11:27, Thomas Bock wrote:
...
I can not understand why the expression in

the subject does not work correct:
 dcrn[which(fn == inve[2])]
numeric(0)

 inve[2]

[1] 406.7

...
  1.) `==' comparisons have a certain tolerance
  2.) the print output is not necessarily precisely your number
Instead of using `==' you should use a comparison with a certain 
tolerance you can specify...
I usually specify...

tol - sqrt(.Machine$double.eps)
dcrn[(fn - inve[2])  tol]
See ?.Machine for details.

Cheers

Jason
--
Indigo Industrial Controls Ltd.
http://www.indigoindustrial.co.nz
64-21-343-545
[EMAIL PROTECTED]
__
[EMAIL PROTECTED] mailing list
https://www.stat.math.ethz.ch/mailman/listinfo/r-help


Re: [R] Why does a[which(b == c[d])] not work?

2003-10-08 Thread Jason Turner
Whoops.  Hit send too quickly.

Jason Turner wrote:
tol - sqrt(.Machine$double.eps)
dcrn[(fn - inve[2])  tol]
that should be
dcrn[abs(fn - inve[2])  tol]
--
Indigo Industrial Controls Ltd.
http://www.indigoindustrial.co.nz
64-21-343-545
[EMAIL PROTECTED]
__
[EMAIL PROTECTED] mailing list
https://www.stat.math.ethz.ch/mailman/listinfo/r-help


Re: [R] Why does a[which(b == c[d])] not work?

2003-10-08 Thread Richard A. O'Keefe
Achim Zeileis [EMAIL PROTECTED] wrote:
R x - 406.7 + 1e-20 
R x
[1] 406.7
R x == 406.7
[1] TRUE

that is
  1.) `==' comparisons have a certain tolerance

No, all.equal() supports tolerance, == does not.

Consider
 .Machine$double.eps 
[1] 2.220446e-16

That is, the smallest relative difference that can be represented on
my (fairly typical IEEE-conforming) machine is 2 in 10 to the 16th.

1e-20/406.7 is 2.458815e-23, which is a factor of 10 million too small
a relative difference for the hardware to be able to represent.  So in

x - 406.7 + 1e-20

the value of x is identical to 406.7 in every bit.

Instead of using `==' you should use a comparison with a certain 
tolerance you can specify...

Such as ?all.equal

__
[EMAIL PROTECTED] mailing list
https://www.stat.math.ethz.ch/mailman/listinfo/r-help