ressw m@iii@g oii meer@@et
2025-Jul-25 20:26 UTC
[R] Delete an object which is named indirectly.
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)
?s 21:26 de 25/07/2025, ressw--- via R-help escreveu:> 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 at 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.Hello, Just use rm's argument 'list'. junk.A = -9999 junk.B = "junk.A" rm(list = junk.B) junk.A #> Error: object 'junk.A' not found junk.B #> [1] "junk.A" Hope this helps, Rui Barradas
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 at 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 at 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.