[R] Delete an object which is named indirectly.
Bert Gunter
bgunter@4567 @end|ng |rom gm@||@com
Sat Jul 26 12:36:27 CEST 2025
Without trying to detract from Rui's and Jeff's advice, here is a clumsy
alternative in the spirit of R's "computing on the language" features that
you seemed to be trying to employ:
> junk.A = -9999
> junk.B = "junk.A"
>
> eval(bquote(rm(.(junk.B))))
> junk.B
[1] "junk.A"
> junk.A
Error: object 'junk.A' not found
See ?bquote or ?substitute. But without knowing more about your context,
consider this advice a sort of "homework" exercise rather than a good
alternative to the use of lists that was already suggested.
Cheers,
Bert
On Sat, Jul 26, 2025 at 1:09 AM Jeff Newmiller via R-help <
r-help using r-project.org> wrote:
> With a better picture of what OP is trying to accomplish we could suggest
> the use of functions as a way to prevent the global environment from
> getting cluttered in the first place.
>
> On July 26, 2025 12:39:44 AM PDT, Rui Barradas <ruipbarradas using sapo.pt>
> wrote:
> >Hello,
> >
> >Good point, rm should be reserved for interactive use only.
> >I didn't thought of lists but if the OP wants to delete junk.A it should
> be
> >
> >
> >dta <- list(junk.A = -9999, junk.B = "junk.A")
> >dta[dta[["junk.B"]]] <- NULL
> >dta
> >#> $junk.B
> >#> [1] "junk.A"
> >
> >
> >
> >Hope this helps,
> >
> >Rui Barradas
> >
> >
> >
> >Às 08:21 de 26/07/2025, Jeff Newmiller via R-help escreveu:
> >> Don't take this the wrong way, but if you are writing code that behaves
> like this then you are doing no-one any favors.
> >>
> >> Just stop messing with variables and start learning how to work with
> lists.
> >>
> >> dta <- list(junk.A = -9999, junk.B="junk.B")
> >> dta[dta[["junk.B"]]] <- NULL
> >> dta
> >>
> >>
> >> On July 25, 2025 1:26:23 PM PDT, ressw--- via R-help <
> r-help using r-project.org> wrote:
> >>> Make two objects
> >>>
> >>> junk.A = -9999
> >>> junk.B = "junk.A"
> >>>
> >>> rm(junk.B) removes junk.B and not junk.A, as it should.
> >>>
> >>> Is there a function, e,g, "rm2", such that
> >>> rm2(junk.B) will delete junk.A and not junk.B?
> >>>
> >>> Why doesn't this work?:
> >>>> rm(eval(junk.B))
> >>> Error in rm(eval(junk.B)) : ... must contain names or character strings
> >>> since eval(junk.B) yields "junk.A"
> >>> and
> >>>> rm("junk.A")
> >>> does work?
> >>>
> >>> R version 4.3.0 (2023-04-21)
> >>>
> >>> ______________________________________________
> >>> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> >>> https://stat.ethz.ch/mailman/listinfo/r-help
> >>> PLEASE do read the posting guide
> https://www.R-project.org/posting-guide.html
> >>> and provide commented, minimal, self-contained, reproducible code.
> >>
> >
>
> --
> Sent from my phone. Please excuse my brevity.
>
> ______________________________________________
> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> https://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
[[alternative HTML version deleted]]
More information about the R-help
mailing list