I was wondering if there is a way to pass the "name" of a variable in R. What I want is to make the following function more generic:
a <- "old"
test <- function () {
assign("a", "new", envir = .GlobalEnv)
}
test()
a
What I don't want is a function that only works if the variable I want to change is called "a", so I was wondering if I can do something like passing the variable name as an argument and then call the assign function with that name. Something like this:
a <- "old"
test <- function (varName) {
assign(varName, "new", envir = .GlobalEnv)
}
test(a) #!!!!! Here !!!!!
a
Thanks for your help.
test(varName = 'a')?afromoldtonewor you want to assign the value ofnewto a new variableold(if its the latter, your function does that, the former is what<-is for...)? Also, if you find yourself usingassignyou're almost always doing it wrong.assignand passing environments around...assignthat takes a variable, not a character. Put another way, I think he's looking for a variation of<-that takes an environment argument.