I'm confused regarding the definition of [symbol]
1. In the following example, my understanding is that h
is a variable and an object, hs
is the name or symbol of h
, but why hs
is '12
' and not h
?
h=12
is.symbol(h) # FALSE
hs=as.symbol(h)
is.symbol(hs) # TRUE
hs # `12`
eval(hs) # Error in eval(hs) : object '12' not found
it's because as.name
(identical to as.symbol
) converts the value stored in h
to a symbol, it doesn't act on the name h
itself. (Which is already a name, so why are we coercing to a name...?)
I think you're looking for something closer to the functionality of substitute()
:
h <- 12
# Converts the value stored in h to a symbol
hs1 <- as.name(h)
str(hs1) # symbol 12
eval(hs1) # Error in eval(hs1) : object '12' not found
hs2 <- substitute(h)
str(hs2) # symbol h
eval(hs2) # 12