Re: [Rd] Match .3 in a sequence

2009-03-18 Thread William Dunlap
> -Original Message- > From: r-devel-boun...@r-project.org > [mailto:r-devel-boun...@r-project.org] On Behalf Of Duncan Murdoch > Sent: Tuesday, March 17, 2009 12:15 PM > To: Daniel Murphy > Cc: r-devel@r-project.org > Subject: Re: [Rd] Match .3 in a sequence >

Re: [Rd] Match .3 in a sequence

2009-03-17 Thread Duncan Murdoch
On 3/17/2009 11:26 AM, Daniel Murphy wrote: Is this a reasonably fast way to do an approximate match of a vector x to values in a list? match.approx <- function(x,list,tol=.0001) sapply(apply(abs(outer(list,x,"-")) If you are willing to assume that the list values are all multiples of 2*t

Re: [Rd] Match .3 in a sequence

2009-03-17 Thread Daniel Murphy
Is this a reasonably fast way to do an approximate match of a vector x to values in a list? match.approx <- function(x,list,tol=.0001) sapply(apply(abs(outer(list,x,"-"))wrote: > Well, first of all, seq(from=.2,to=.3) gives c(0.2), so I assume you > really mean something like seq(from=.2,to=

Re: [Rd] Match .3 in a sequence

2009-03-17 Thread Petr Savicky
On Tue, Mar 17, 2009 at 10:04:39AM -0400, Stavros Macrakis wrote: ... > 1) Factor allows repeated levels, e.g. factor(c(1),c(1,1,1)), with no > warning or error. Yes, this is a confusing behavior, since repeated levels are never meaningful. > 2) Even from distinct inputs, factor of a numeric vect

Re: [Rd] Match .3 in a sequence

2009-03-17 Thread Stavros Macrakis
Petr, Thank you for the detailed diagnosis of the bizarre behavior I reported, which seems to indicate several distinct problems in the underlying code: 1) Factor allows repeated levels, e.g. factor(c(1),c(1,1,1)), with no warning or error. 2) Even from distinct inputs, factor of a numeric vecto

Re: [Rd] Match .3 in a sequence

2009-03-17 Thread Wacek Kusnierczyk
Wacek Kusnierczyk wrote: > > > there's one more curiosity about factors, in particular, ordered factors: > > ord <- as.ordered(nums); ord > # [1] 0.300 0.3 0.3 > 0.300 > # Levels: 0.300 < 0.3 < 0.3 < 0.300 > >

Re: [Rd] Match .3 in a sequence

2009-03-17 Thread Petr Savicky
On Tue, Mar 17, 2009 at 10:15:39AM +0100, Wacek Kusnierczyk wrote: ... > there's one more curiosity about factors, in particular, ordered factors: > > ord <- as.ordered(nums); ord > # [1] 0.300 0.3 0.3 > 0.300 > # Levels: 0.30

Re: [Rd] Match .3 in a sequence

2009-03-17 Thread Wacek Kusnierczyk
Petr Savicky wrote: > On Mon, Mar 16, 2009 at 07:39:23PM -0400, Stavros Macrakis wrote: > ... > >> Let's look at the extraordinarily poor behavior I was mentioning. Consider: >> >> nums <- (.3 + 2e-16 * c(-2,-1,1,2)); nums >> [1] 0.3 0.3 0.3 0.3 >> >> Though they all print as .3 with the default

Re: [Rd] Match .3 in a sequence

2009-03-16 Thread Petr Savicky
On Mon, Mar 16, 2009 at 07:39:23PM -0400, Stavros Macrakis wrote: ... > Let's look at the extraordinarily poor behavior I was mentioning. Consider: > > nums <- (.3 + 2e-16 * c(-2,-1,1,2)); nums > [1] 0.3 0.3 0.3 0.3 > > Though they all print as .3 with the default precision (which is > normal and

Re: [Rd] Match .3 in a sequence

2009-03-16 Thread Daniel Murphy
Got it. Thank you all. On Mon, Mar 16, 2009 at 4:39 PM, Stavros Macrakis wrote: > The factor approach is horrifically ugly and dangerous. > > Even if it didn't have the extraordinarily poor behavior documented > below, it simply isn't well-defined what it should do. The explicit > approximation

Re: [Rd] Match .3 in a sequence

2009-03-16 Thread Stavros Macrakis
The factor approach is horrifically ugly and dangerous. Even if it didn't have the extraordinarily poor behavior documented below, it simply isn't well-defined what it should do. The explicit approximation route is far far preferable in every way: more predictable, more controllable, and even (th

Re: [Rd] Match .3 in a sequence

2009-03-16 Thread Daniel Murphy
I have a matrix whose columns were filled with values which were functions of cvseq<-seq(.2,.3,by=.1) (and a row value of mode integer). To do a lookup for cv=.3 later, I wanted to match(.3,cvseq), which gave me NA, hence my question. I thought R would match .3 in cvseq within .Machine$double.eps,

Re: [Rd] Match .3 in a sequence

2009-03-16 Thread Wacek Kusnierczyk
Petr Savicky wrote: > On Mon, Mar 16, 2009 at 06:36:53AM -0700, Daniel Murphy wrote: > >> Hello:I am trying to match the value 0.3 in the sequence seq(.2,.3). I get >> >>> 0.3 %in% seq(from=.2,to=.3) >>> >> [1] FALSE >> > > As others already pointed out, you should use seq(from=

Re: [Rd] Match .3 in a sequence

2009-03-16 Thread Petr Savicky
On Mon, Mar 16, 2009 at 06:36:53AM -0700, Daniel Murphy wrote: > Hello:I am trying to match the value 0.3 in the sequence seq(.2,.3). I get > > 0.3 %in% seq(from=.2,to=.3) > [1] FALSE As others already pointed out, you should use seq(from=0.2,to=0.3,by=0.1) to get 0.3 in the sequence. In order to

Re: [Rd] Match .3 in a sequence

2009-03-16 Thread Stavros Macrakis
Well, first of all, seq(from=.2,to=.3) gives c(0.2), so I assume you really mean something like seq(from=.2,to=.3,by=.1), which gives c(0.2, 0.3). %in% tests for exact equality, which is almost never a good idea with floating-point numbers. You need to define what exactly you mean by "in" for flo

Re: [Rd] Match .3 in a sequence

2009-03-16 Thread Wacek Kusnierczyk
Duncan Murdoch wrote: > On 3/16/2009 9:36 AM, Daniel Murphy wrote: >> Hello:I am trying to match the value 0.3 in the sequence seq(.2,.3). >> I get >>> 0.3 %in% seq(from=.2,to=.3) >> [1] FALSE >> Yet >>> 0.3 %in% c(.2,.3) >> [1] TRUE >> For arbitrary sequences, this "invisible .3" has been problema

Re: [Rd] Match .3 in a sequence

2009-03-16 Thread Duncan Murdoch
On 3/16/2009 9:36 AM, Daniel Murphy wrote: Hello:I am trying to match the value 0.3 in the sequence seq(.2,.3). I get 0.3 %in% seq(from=.2,to=.3) [1] FALSE Yet 0.3 %in% c(.2,.3) [1] TRUE For arbitrary sequences, this "invisible .3" has been problematic. What is the best way to work around thi

[Rd] Match .3 in a sequence

2009-03-16 Thread Daniel Murphy
Hello:I am trying to match the value 0.3 in the sequence seq(.2,.3). I get > 0.3 %in% seq(from=.2,to=.3) [1] FALSE Yet > 0.3 %in% c(.2,.3) [1] TRUE For arbitrary sequences, this "invisible .3" has been problematic. What is the best way to work around this? Thank you. Dan [[alternative HTML