On 18/09/2019 8:43 a.m., Huzefa Khalil wrote:
Hello R-users,

I have been running a script which produces objects based on the
column names of a data.frame. The column names are of the form CB_1-1,
CB_1-2, etc. Now this calculation was rather long and memory
intensive, so I would rather not have to do it again after fixing the
column names using "make.names". As a consequence, I am left with a
bunch of R objects with `-` in the name.
Accessing them is proving challenging and any help would be appreciated.

Reproducible example:
`cb_1-2` <- "hello world"
t <- "cb_1-2"
t <- as.name(t)
t <- eval(parse(text = t))

Error in eval(parse(text = t)) : object 'cb_1' not found

After t <- as.name(t), you already have language: no need to parse it again. So

  eval(t)

works. If you have more complicated expressions, use call() to set them ụp. For example, call("paste0", t, "!") evaluates to

  paste0(`cb_1-2`, "!")

and evaluating that expression via

  eval(call("paste0", t, "!"))

gives

[1] "hello world!"

Don't go back and forth between language objects and text representations of them, because it's hard to do that without introducing changes. In other words, don't use eval(parse()).

Duncan Murdoch

______________________________________________
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
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.

Reply via email to