如何分配给R中变量值的names()属性

发布于 2024-09-03 14:28:01 字数 204 浏览 9 评论 0原文

在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

寒冷纷飞旳雪 2024-09-10 14:28:01

在您提出问题的表单中,无需分配名称。如果你的x存在,那么你就执行names(x) <- v。这是执行此操作的正确方法。

如果你的变量名未知(即动态创建),那么你可以使用substitute

nm <- "xxx" # name of your variable
v <- 1:3 # value
assign(nm,v) # assign value to variable

w <- c("a","b","c") # names of variable
eval(substitute(names(x)<-w, list(x=as.symbol(nm))))
# Result is
str(xxx)
# Named int [1:3] 1 2 3
# - attr(*, "names")= chr [1:3] "a" "b" "c"

,但是如果你必须做这种技巧,那么你的代码就有问题了。

In the form you ask question there is no need to assign names. If you x exists then you do names(x) <- v. This is right way to do this.

If your variable name is unknown (i.e. dynamically created) then you could use substitute

nm <- "xxx" # name of your variable
v <- 1:3 # value
assign(nm,v) # assign value to variable

w <- c("a","b","c") # names of variable
eval(substitute(names(x)<-w, list(x=as.symbol(nm))))
# Result is
str(xxx)
# Named int [1:3] 1 2 3
# - attr(*, "names")= chr [1:3] "a" "b" "c"

But if you must do this kind of tricks there is something wrong with you code.

国产ˉ祖宗 2024-09-10 14:28:01

试试这个:

assign(paste(names(x),collapse="."), v)

如果有多个名称,请使用 collapse 代替。

> v <- 1:10
> names(v) <- letters[1:10]
> v
 a  b  c  d  e  f  g  h  i  j 
 1  2  3  4  5  6  7  8  9 10 
> assign(paste(names(v), collapse=""), v)
> abcdefghij
 a  b  c  d  e  f  g  h  i  j 
 1  2  3  4  5  6  7  8  9 10

Try this:

assign(paste(names(x),collapse="."), v)

Use collapse instead if there are multiple names.

> v <- 1:10
> names(v) <- letters[1:10]
> v
 a  b  c  d  e  f  g  h  i  j 
 1  2  3  4  5  6  7  8  9 10 
> assign(paste(names(v), collapse=""), v)
> abcdefghij
 a  b  c  d  e  f  g  h  i  j 
 1  2  3  4  5  6  7  8  9 10
紫﹏色ふ单纯 2024-09-10 14:28:01

马雷克的答案有效,但阿尼科的问题是一个简单的答案。

nm <- "xxx"; v<- 1:3; names(v) <- c("a","b","c"); assign(nm,v)

这是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.

nm <- "xxx"; v<- 1:3; names(v) <- c("a","b","c"); assign(nm,v)

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!

┈┾☆殇 2024-09-10 14:28:01

如果变量的名称作为字符串存储在其他变量(variable_name)中,我将执行以下操作。

temp <- get(variable_name)

names(temp)<- array_of_names

assign(variable_name,temp)

If the name of the variable is stored as a string in other variable (variable_name), I will do the following.

temp <- get(variable_name)

names(temp)<- array_of_names

assign(variable_name,temp)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文