从全局环境中排除特定的对象类型 [英] Exclude specific object type from the global environment

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

问题描述

我在我的全球环境中加载了许多不同的对象。如何仅排除数据帧并保留其他对象?示例:

  DF1<  -  data.frame(rnorm(10))
DF2 < - data.frame (rnorm(10))
DF3< - data.frame(rnorm(10))

list1< - list(a,b,c b $ b list2< - list(a,b,c)
tf< - tempfile()
td< - tempdir()

我想到的解决方案看起来像这样(当然不起作用)

  remove(pattern =*。Rdata)


解决方案

这是一个我用于这样的任务的函数。 rmSome()只会从环境中删除对象的一些。它通过应用其第一个参数中给出的函数(即 is * 函数,如 is.data.frame()用于数据帧, is.list()用于列表等)到给定环境中的对象列表,并过滤出结果。

  rmSome<  -  function(FUN,env = globalenv(),negate = FALSE){
fun< - match.fun FUN)
if(negate)fun< - 否定(fun)
objget< - mget(ls(envir = env),envir = env)
rmnames< (fun,objget))
rm(list = rmnames,envir = env)
}

例如,您可以使用

  rmSome(is.data.frame)删除全局环境中的所有数据框

所以对于你给定的例子,你可以删除所有的数据框架,如下所示:

  ##  -  rm(list = ls())这里 -  
##定义rmSome()这里
DF1< - data.frame(rnorm(10))
DF2 < data.frame(rnorm(10))
DF3< - data.frame(rnorm(10))
list1< - list(a,b,c)
list2 < - list(a,b,c)
tf< - tempfile()
td< - tempdir()

##删除所有数据帧
rmSome(is.data.frame)
ls()
#[1]list1list2rmSometdtf

另一方面,如果你想保留所有的数据框架并删除所有其他内容,你会否定删除这样的数据框:

  rmSome(is.data.frame,negate = TRUE)

到目前为止,我还没有发现任何使用其他功能的问题,如 is.numeric() is.environment()等用于删除数字,环境等。但是该功能当前没有设置来处理多个对象类型一次。



更新1/28/2015 eapply() can也可用于将功能应用于环境。如果您不喜欢 mget(),则可以使用第二个函数。它可以与上面的调用一样,可能是更好的方法。

  rmSome2<  -  function FUN,env = globalenv(),negate = FALSE){
fun < - match.fun(FUN)
if(negate)fun< - 否定(fun)
ue& - unlist(eapply(env,fun))
rm(list = names(ue)[ue],envir = env)
}


I have many different objects loaded in my global environment. How can I exclude only the data frames and keep the other objects? Example:

DF1 <- data.frame(rnorm(10))
DF2 <- data.frame(rnorm(10))
DF3 <- data.frame(rnorm(10))

list1 <- list("a", "b", "c")
list2 <- list("a", "b", "c")
tf <- tempfile()
td <- tempdir()

The solution I had in mind looked something like this (of course it didn't work)

remove(pattern="*.Rdata")

解决方案

Here is a function I use for tasks like this. rmSome() does just that, removes only some of the objects from an environment. It does this by applying the function given in its first argument (i.e. an is* function like is.data.frame() for data frames, is.list() for lists, etc.) to the list of objects in the given environment and filtering out the results.

rmSome <- function(FUN, env = globalenv(), negate = FALSE) {
    fun <- match.fun(FUN)
    if(negate) fun <- Negate(fun)
    objget <- mget(ls(envir = env), envir = env)
    rmnames <- names(Filter(fun, objget))
    rm(list = rmnames, envir = env)
}

For example, you can remove all data frames from the global environment with

rmSome(is.data.frame)

So for your given example, you can remove the all the data frames like this:

## -- rm(list=ls()) here --
## Define rmSome() here 
DF1 <- data.frame(rnorm(10))
DF2 <- data.frame(rnorm(10))
DF3 <- data.frame(rnorm(10))
list1 <- list("a", "b", "c")
list2 <- list("a", "b", "c")
tf <- tempfile()
td <- tempdir()

## remove all data frames
rmSome(is.data.frame)
ls()
# [1] "list1"  "list2"  "rmSome" "td"     "tf"    

On the other hand, if you wanted to keep all the data frames and remove everything else, you would negate the removal of data frames like this:

rmSome(is.data.frame, negate = TRUE)

So far I haven't found any issues with using the other functions like is.numeric(), is.environment(), etc. for removing numerics, environments, etc. But the function is not currently set up to handle more than one object type at a time.

Update 1/28/2015: eapply() can also be used for applying a function to an environment. Here's a second function you could use if you don't like mget(). It can be used just the same as the calls above and is probably the better way to go.

rmSome2 <- function(FUN, env = globalenv(), negate = FALSE)  {
    fun <- match.fun(FUN)
    if(negate) fun <- Negate(fun)
    ue <- unlist(eapply(env, fun))
    rm(list = names(ue)[ue], envir = env)
}

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

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