如何查找所有附加的数据框?

Pab*_*yan 2 r attachment dataframe

当我在R studio中附加data.frame时,我收到以下消息:

以下物体被掩盖......

我忘了分离data.frame

data<-read.table(file.choose(),header=TRUE)
View(data)
attach(data) 
## The following objects are masked from vih (pos = 3):
## edad, edadg, id, numpares, numparg, sifprev, udvp, vih 
## The following objects are masked from vih (pos = 4):
## edad, edadg, id, numpares, numparg, sifprev, udvp, vihhere
Run Code Online (Sandbox Code Playgroud)

有没有办法知道哪些data.frames附加?

有没有办法用一个命令或函数分离所有data.frames?

MrF*_*ick 8

首先,我建议你停止使用attach().这的确是一个不好的做法,因为几乎总有更好的选择(with()data=例如参数)

但你可以看到附加的对象

search()
Run Code Online (Sandbox Code Playgroud)

如果您假设所有data.frame名称都不以"."开头.并且不包含":",你可以将它们全部分开

detach_dfs1 <- function() {
    dfs <- grep("^\\.|.*[:].*|Autoloads", search(), invert=T)
    if(length(dfs)) invisible(sapply(dfs, function(x) detach(pos=x)))
}
Run Code Online (Sandbox Code Playgroud)

或者如果您假设data.frames在全局环境中,您可以这样做

detach_dfs2 <- function() {
    dfs <- Filter(function(x) exists(x) && is.data.frame(get(x)), search())
    if(length(dfs)) invisible(sapply(dfs, function(x) detach(x, character.only=T)))
}
Run Code Online (Sandbox Code Playgroud)