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, Hyunchul [[alternative HTML version deleted]]
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<y) HTH Ivan Le 9/3/2010 15:33, Hyunchul Kim a écrit :> 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, > > Hyunchul > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >-- Ivan CALANDRA PhD Student University of Hamburg Biozentrum Grindel und Zoologisches Museum Abt. Säugetiere Martin-Luther-King-Platz 3 D-20146 Hamburg, GERMANY +49(0)40 42838 6231 ivan.calandra@uni-hamburg.de ********** http://www.for771.uni-bonn.de http://webapp5.rrz.uni-hamburg.de/mammals/eng/mitarbeiter.php [[alternative HTML version deleted]]
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> sign(x - .1)[1] -1 So how you implement this will depend on what you need it for. Something like this should work, if you choose the appropriate level of precision:> sign(round(x - .1, 9))[1] 0 Sarah On Fri, Sep 3, 2010 at 9:33 AM, Hyunchul Kim <hyunchul.kim.sfc at gmail.com> 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) > } > > Thanks in advance, > > Hyunchul >-- Sarah Goslee http://www.functionaldiversity.org
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
> 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 <hyunchul.kim.sfc at gmail.com> 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) > } > > Thanks in advance, > > Hyunchul > > ? ? ? ?[[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >-- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem that you are trying to solve?