On Sep 3, 2010, at 9:33 AM, Hyunchul Kim wrote:
Hi, all
is there a built-in function to compare two numbers?
something like following function
cmp <- function(x, y){
value <- 0
if (x > y){
value <- 1
}else if (x == y){
value <- 0
}else {
value <- -1
}
r
> x <- 2
> y <- 3
> ifelse(x > y, 1, ifelse(x < y, -1, 0))
[1] -1
On Fri, Sep 3, 2010 at 9:33 AM, Hyunchul Kim wrote:
> Hi, all
>
> is there a built-in function to compare two numbers?
>
> something like following function
>
> cmp <- function(x, y){
> value <- 0
> if (x > y){
> valu
You could potentially use sign()
> sign(3 - 5)
[1] -1
> sign(5 - 3)
[1] 1
> sign(5 - 5)
[1] 0
But... this could fail when you think two numbers are equal and the
computer doesn't, due to floating point precision. (Your version could
fail in exactly the same way.)
> x <- .3 - .2
> x
[1] 0.1
> sig
Hi!
Maybe something like this:
x <- 2
y <- 3
#since FALSE will be converted to 0 and TRUE to 1 you can do
as.numeric(x>y)
as.numeric(x Hi, all
>
> is there a built-in function to compare two numbers?
>
> something like following function
>
> cmp<- function(x, y){
> value<- 0
> if (x>
Hi, all
is there a built-in function to compare two numbers?
something like following function
cmp <- function(x, y){
value <- 0
if (x > y){
value <- 1
}else if (x == y){
value <- 0
}else {
value <- -1
}
return(value)
}
Thanks in advance,
Hyunchu
5 matches
Mail list logo