帮助文件调用attributes()
一个功能。它的语法看起来像一个函数调用。连class(attributes)
称之为函数。
但我知道我可以为 attributes(myobject)
分配一些东西,这似乎不寻常。例如,我无法将任何内容分配给 log(myobject)
.
那么像 attributes()
这样的“函数”的正确名称是什么? ?还有其他例子吗?你如何区分它们与常规函数的区别? (除了尝试 supposedfunction(x)<-0
之外。)
最后,我猜attributes()
实现覆盖赋值运算符,以便成为赋值的目的地。我对吗?是否有关于如何操作的实用指南?
最佳答案
确实很好的观察。这是一个替换函数的例子,如果你仔细观察并输入apropos('attributes')
在你的 R 控制台中,它将返回
"attributes"
"attributes<-"
连同其他输出。
所以,基本上你可以在赋值运算符左边的符号上赋值的地方,你不是在调用 attributes
, 你实际上是在调用 attributes<-
, R 中有很多这样的函数,例如:names(), colnames(), length()
等等。在你的例子中log
没有任何替代品,因此它无法按您预期的方式工作。
定义(来自下面给出的高级 R 书籍链接):
Replacement functions act like they modify their arguments in place, and have the special name xxx<-. They typically have two arguments (x and value), although they can have more, and they must return the modified object
如果您想查看这些功能的列表,您可以执行以下操作:
apropos('<-$')
您可以查看具有相似属性的相似函数。
我希望这能解决您的问题。
关于r - attributes() 是 R 中的一个函数吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54166550/