[R] function to compare numbers

David Winsemius dwinsemius at comcast.net
Fri Sep 3 15:55:35 CEST 2010


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
>    }
>    return(value)
> }
>

Not that I know of, but there is an obvious application of sign():

cmp <-function(x,y) sign(y-x)


--

David Winsemius, MD
West Hartford, CT



More information about the R-help mailing list