I have a function that uses readline
to allow the user to enter the name they want to give for a variable I will be creating for them. Let's call this "USER.DEFINED.VARIABLE". It contains the name I want to use for another variable. Let's say that "USER.DEFINED.VARIABLE" gets set by readline
to be "jimsfilename".
I know I can assign value to a variable named "jimsfilename" using:
assign(USER.DEFINED.VARIABLE,c(1,2,3,4,5))
"jimsfilename" will now have 1,2,3,4,5
in it. However, how do I now fuss with "jimsfilename", given that I don't (before readline assigns it to USER.DEFINED.VARIABLE) know what its name is?
In other words, lets say I now want to add 1 to every value in jimsfilename. I can't do:
USER.DEFINED.VARIABLE <- USER.DEFINED.VARIABLE + 1 # can't do this
because "USER.DEFINED.VARIABLE" is actually a text string name. I want instead to refer to jimsfilename, but all I have is USER.DEFINED.VARIABLE to indicate it. I'm sure this is something easy...
Variable Names Rules for R variables are: A variable name must start with a letter and can be a combination of letters, digits, period(.) and underscore(_). If it starts with period(.), it cannot be followed by a digit.
By default, R does not allow duplicate column names in a data frame. However, by using the check. names = FALSE option we still can generate such a data frame.
No, it is not. Because they both are in different scope. x outside of main function has class level scope while x inside of main has method/function level scope. It is legal for 2 variables in different scope to have same name.
I'll just say it once more: if you need to rename variables in R, just use the rename() function.
It depends bit on what you want to do, but here's an example of using get
function:
x = 1
get("x") + 1
2
assign("name", get("x") + 1)
name
2
Why not just do all the manipulation of the variable (adding 1 or other changes) to a local copy of the variable with your own name, then at the end of the function/script/whatever do the assigning or other saving? That would be much simpler than creating the variable then having to use get
to get a copy, change it, and assign
it again.
Even better is to use your own variable name inside of a function, then just return the result and let the user decide what to name it at that point. This is the much more Rish way of doing things, it is best to not use the assign
function at all. Most things that can be done using assign
can be done much simpler by using a list and subscripting.
Functions should not change anything in the global environment, just return any values that the user might need and let the user make the assignment.
You can use eval
and parse
. The later interprets text as if it was an input in the console. The first evaluates the expression (generated by parse
, for instance). Example:
> varname <- "user.defined.variable"
> varvalue <- 42
> eval(parse(text=paste(varname, varvalue, sep=" <- ")), envir=.GlobalEnv)
> ls()
[1] "user.defined.variable" "varname" "varvalue"
> user.defined.variable
[1] 42
Note that I've choosen the global environment as the destination for the new variable. You can make the appropriate changes if that is not the case.
To refer to the new variable later, you can use as.symbol
. Just evaluate it under the environment where you assigned the new variable:
> eval(as.symbol(varname), envir=.GlobalEnv)
[1] 42
You can also use substitute
to create expressions that eval
can understand:
> eval(substitute(x+1, list(x=as.symbol(varname))), envir=.GlobalEnv)
[1] 43
To make changes to the new variable, just creat assignments expressions and evaluate them:
> eval(substitute(x <- x*10, list(x=as.symbol(varname))), envir=.GlobalEnv)
> eval(as.symbol(varname), envir=.GlobalEnv)
[1] 420
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With