如何分配给R中变量值的names()属性
在 R 中,“assign('x',v)”将名称为“x”的对象设置为 v。将“x”替换为将文本函数应用于变量 x 的结果。那么“赋值”就显示出了它的价值。
不幸的是,“分配(粘贴('names(','x',')',sep =''),v)”失败。因此,如果“x”是变量 x,我可以设置它的值,但不能为其元素指定名称。
可以解决这个问题吗?也许是解析评估技巧?谢谢。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
在您提出问题的表单中,无需
分配
名称。如果你的x
存在,那么你就执行names(x) <- v
。这是执行此操作的正确方法。如果你的变量名未知(即动态创建),那么你可以使用
substitute
,但是如果你必须做这种技巧,那么你的代码就有问题了。
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.
试试这个:
如果有多个名称,请使用
collapse
代替。Try this:
Use
collapse
instead if there are multiple names.马雷克的答案有效,但阿尼科的问题是一个简单的答案。
这是Aniko的回答,她应该得到信任。
我使用它的情况有 >1 个查询类,每个类都有不同的变量名,每个类包含 >1 个 sql 查询。例如,查询类名称为“config_query”,列表中包含三个命名查询,例如“q1”、“q2”、“q3”。并进一步查询类名。我想创建一个循环,它将查询类名称的根前缀(例如“config”表示“config_query”)作为列表,获取其查询内容,运行查询,并在结果类变量名称中列出结果数据帧例如“config_result”,这样“config_result”中的每个结果都与“config_query”中的查询具有相同的名称,它是其结果。
换句话说,我想要免费的结果类变量名和相应的名称映射,给定根前缀和初始查询。使用 allocate() 分配给结果类变量名。我被困在如何进行名称映射上。谢谢!
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!
如果变量的名称作为字符串存储在其他变量(variable_name)中,我将执行以下操作。
If the name of the variable is stored as a string in other variable (variable_name), I will do the following.