2

In a larger R-Script I have to address the content of a character variable that itself is stored within a variable of class name. In the following small example, how do I have to address B to see the content of A (thus "A") and not just the name (thus A)?

> A <- "A"
> B <- as.name(A)
> B
A

1 Answer 1

2

We can use get to the retrieve the value

get(A)

From 'B', we can use eval

eval(B)
#[1] "A"

The OP's example is confusing. Just to understand it better,

C <- "A"
B <- as.name(C)
eval(B)
#[1] "A"
Sign up to request clarification or add additional context in comments.

2 Comments

@user7417 I am using R 3.3.0 and this is the output I am getting.
Sorry, my comment was mutilated and I did not edit it in time. The second line must be B <- as.name("C") otherwise I get the message "Error in eval(expr, envir, enclos) : Object 'A' not found". Anyway, thanks for the eval hint, I'll check it out in my script!

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.