gpt4 book ai didi

r - 为环境分配属性会导致意外?

转载 作者:行者123 更新时间:2023-12-02 08:31:21 25 4
gpt4 key购买 nike

根据R语言定义:

Unlike most other R objects, environments are not copied when passed to functions or used in assignments. Thus, if you assign the same environment to several symbols and change one, the others will change too. In particular, assigning attributes to an environment can lead to surprises.

> e = new.env()
> attr(e, "mynumber", TRUE)
NULL
> attr(e, "mynumber") = 555
> attr(e, "mynum")
[1] 555

究竟有哪些惊喜?

最佳答案

例如,我们将 2 分配给 eat 属性和 fat 属性也会自动更改:

e <- new.env()
attr(e, "at") <- 1
f <- e
attr(e, "at") <- 2
attributes(f)
## $at
## [1] 2

关于r - 为环境分配属性会导致意外?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26529986/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com