In R, "assign('x',v)" sets the object whose name is 'x' to v. Replace 'x' by the result of applying a text function to a variable x. Then "assign" shows its worth.
Unfortunately, "assign(paste('names(','x',')',sep=''),v)" fails. So if 'x' is a variable x, I can set its value, but I can't give it names for its elements.
Can one work around this? a parse-eval trick maybe? Thanks.
Try this:
Use
collapse
instead if there are multiple names.Marek's answer works, but Aniko's question is a simple answer.
This is Aniko's answer, she should get credit.
The case I use this for has >1 classes of queries, each with a different varname, and each class containing >1 sql query. So, say, a query class name of "config_query" with three named queries in a list, say "q1", "q2", "q3". And further query class names. I want to make a loop that will take the root prefixes (such as "config" for "config_query") of query class names as a list, get their query contents, run the queries, and list the result data frames in result class varnames such as "config_result", such that each result in "config_result" has the same name as the query in "config_query" which it's the result of.
Said differently, I want result class varnames and corresponding name mappings for free, given root prefixes and initial queries. Using assign() assigns to result class varnames. I was stuck on how to do the name mappings. Thanks!
In the form you ask question there is no need to
assign
names. If youx
exists then you donames(x) <- v
. This is right way to do this.If your variable name is unknown (i.e. dynamically created) then you could use
substitute
But if you must do this kind of tricks there is something wrong with you code.
If the name of the variable is stored as a string in other variable (variable_name), I will do the following.