Re: [R] converting numeric to integer

2009-05-17 Thread Gabor Grothendieck
Try this: as.integer(x + sign(x) * .5) On Sun, May 17, 2009 at 6:01 AM, Thomas Mang wrote: > Hi, > > The problem is, x might be negative. If x == -6.999, the result should > be -7, not -6. That's why my original proposal had the ifelse-condition (one > could alternatively write sign(x) * 0.

Re: [R] converting numeric to integer

2009-05-17 Thread Thomas Mang
Hi, The problem is, x might be negative. If x == -6.999, the result should be -7, not -6. That's why my original proposal had the ifelse-condition (one could alternatively write sign(x) * 0.5, BTW. I agree however that in my proposal, the round(x) is redundant, one can use x itself as left

Re: [R] converting numeric to integer

2009-05-17 Thread Gabor Grothendieck
Try: as.integer(x + 0.5) assuming the calculation error is less than 0.5 . On Sat, May 16, 2009 at 2:49 PM, Thomas Mang wrote: > Hello, > > Suppose I have x, which is a variable of class numeric. The calculations > performed to yield x imply that mathematically it should be an integer , but > d

Re: [R] converting numeric to integer

2009-05-17 Thread Linlin Yan
On Sun, May 17, 2009 at 5:00 PM, Thomas Mang wrote: > Hi, > > Well, also not quite. > Suppose x = 5.001 (in finite binary represenation). Then I want x == > 5, but that is of course an integer which is now less than the original > numeral. Sorry for that I haven't got what your mean exactl

Re: [R] converting numeric to integer

2009-05-16 Thread Linlin Yan
How about ceiling(x), which return the smallest integer not less than x? On Sun, May 17, 2009 at 2:49 AM, Thomas Mang wrote: > Hello, > > Suppose I have x, which is a variable of class numeric. The calculations > performed to yield x imply that mathematically it should be an integer , but > due t

[R] converting numeric to integer

2009-05-16 Thread Thomas Mang
Hello, Suppose I have x, which is a variable of class numeric. The calculations performed to yield x imply that mathematically it should be an integer , but due to round-off errors, it might not be (and so in either direction). The error is however small, so round(x) will yield the appropriat