[R] simple question about max(x,y)

David Winsemius dwinsemius at comcast.net
Wed May 16 05:14:56 CEST 2012


On May 15, 2012, at 8:42 PM, lapertem4 wrote:

> x <- 3
> y <- 4
> max(x,y)
> 4
> --------------------
> how can i get the greater variable name
> y
>
Option one:
 > test <- function(x,y) { c("x","y")[which.max(c(x,y))]}
 > test(3,4)
[1] "y"
 > test(4,3)
[1] "x"

Option two:
 > test <- function(x,y) { xn <- deparse(substitute(x)); yn <-  
deparse(substitute(y)); c(xn, yn)[which.max(c(x,y))]}
 > test(4,3)
[1] "4"
 > test(x,y)
[1] NA
 > x=4
 > y=3
 > test(x,y)
[1] "x"

-- 

David Winsemius, MD
West Hartford, CT



More information about the R-help mailing list