如何分配给名称()中的R的变量的值的属性 [英] how to assign to the names() attribute of the value of a variable in R

查看:526
本文介绍了如何分配给名称()中的R的变量的值的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在R,分配('X',V)规定,其名称是对象的x为诉由应用文本函数变量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.

不幸的是,分配(糊('名称(','X',')',九月=''),v)的失败。所以,如果X是一个变量x,我可以设置它的价值,但我不能给它的名字为它的元素。

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.

可在一个解决此问题?解析-EVAL把戏可能?谢谢你。

Can one work around this? a parse-eval trick maybe? Thanks.

推荐答案

在你问问题,没有必要为分配名称的形式。如果您 X 存在,那么你做的名称(X)< - v 。这是为了做到这一点正确的方式。

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"

但如果你必须做这样的把戏有什么不对你code。

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

这篇关于如何分配给名称()中的R的变量的值的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆