4

I've searched online for a while now but not really finding answers to this specific question. maybe im not using the right key words... but if there's already a thread on this question, please direct me to it!

so let's say the data looks like this:

a = 3 b = 5

now i want to find out which one is larger, so i do:

max(a,b)

this will only return me the "5", but i want it to return "b" i tried using which() but it keep saying "which is not logical" i also tried deparse() but i get a string of "5"

thanks for any help in advance!

3 Answers 3

2

thanks andrewelamb for a great start on the answer. i changed the code a bit to produce the right answer i was looking for:

`a = 3
 b = 5
 v_name = ["a","b"]
 v_name[which.max(c(a,b))]`

hope it helps anyone else who has this problem as well in the future

Sign up to request clarification or add additional context in comments.

Comments

0
a <- 5
b <- 4
vector <- c(a,b)
which.max(vector)
vector[which.max(vector)]

2 Comments

thank you very much! one thing i'd like to point out is, the a & b passed into the vector should be strings. but when passing into the which.max() should be numbers.
I misunderstood, I think John's got it above
0

A basic approach to get variable name is implemented as myfunc, everything else is concise:

myfunc <- function(v1) {
  deparse(substitute(v1))
}
a=3
b=5
ifelse(max(a,b),myfunc(a),myfunc(b))

2 Comments

what an interesting way to approach this. :)
However, both approaches need one element for each variable to be defined. My: myfunc(variable) (conditional function call), yours: "variable" (in vector) ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.