从R中的环境中删除对象 [英] Deleting objects from environment in R

查看:627
本文介绍了从R中的环境中删除对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读Hadley的高级R.在第8章中,他说我们可以使用 rm()从环境中删除一个对象。但是,删除后,我仍然可以看到该对象。

I am reading Hadley's Advanced R. In chapter 8, he says that we can remove an object from an environment by using rm(). However, I am still able to see the object after removing it.

这是我的代码:

e<- new.env()
e$a<-1
e$b<-2
e$.a<-3
e$c<-4
ls(e,all.names = TRUE)

#remove the object c
rm("c",envir = e)
ls(e,all.names = TRUE) #Doesn't exist here

#does the variable exist?
exists("c",envir = e) #Shows TRUE. Why is this?
exists("m",envir = e) #FALSE

ls(e,all.names = TRUE)
ls(e)

如上所述,理想情况下,我预计存在(c,envir = e) 返回 FALSE

As we can see above, ideally, I'd have expected exists("c", envir = e) to return FALSE.

任何想法?感谢提前。

推荐答案

帮助(存在) p>

From help(exists):


如果继承 TRUE 并且在指定环境中找不到 x 的值,将搜索环境的包围框,直到名称 x 遇到。

If inherits is TRUE and a value is not found for x in the specified environment, the enclosing frames of the environment are searched until the name x is encountered.

命名变量时要小心。您与基础函数 c()有冲突。由于 inherits = TRUE 是默认值,所以搜索包围的环境,在这种情况下,基本函数 c()发现,它产生 TRUE 结果。因此,要仅搜索环境 e 然后退出,请使用 inherits = FALSE

Be careful when naming your variables. You have a conflict with the base function c(). Since inherits = TRUE is the default, the enclosing environments are searched and in this case the base function c() is found, which produces the TRUE result. Therefore, to search only the environment e and then quit, use inherits = FALSE.

exists("c", envir = e, inherits = FALSE)
# [1] FALSE

这篇关于从R中的环境中删除对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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