1) The first line produces a named logical vector, to.rm
with a component for each object which is TRUE if that object should be removed and FALSE otherwise. Thus names(to.rm)[to.rm]
are the objects to be removed so feed that into rm
. By splitting it into two steps, this lets one review to.rm
before actually performing the rm
.
to.rm <- unlist(eapply(.GlobalEnv, function(x) is.data.frame(x) && ncol(x) < 3))
rm(list = names(to.rm)[to.rm], envir = .GlobalEnv)
If this is entered directly into the global environment (i.e. not placed in a fucntion) then envir = .GlobalEnv
in the last line is the default and can be omitted.
2) Another way is to iterate through the object names of env
as shown. We have provided a verbose
argument to show what it is doing and a dryrun
argument to show what it would remove without actually removing anything.
rm2 <- function(env = .GlobalEnv, verbose = FALSE, dryrun = FALSE, all.names = FALSE) {
for(nm in ls(env, all.names = all.names)) {
obj <- get(nm, env)
if (is.data.frame(obj) && ncol(obj) < 3) {
if (verbose || dryrun) cat("removing", nm, "\n")
if (!dryrun) rm(list = nm, envir = env)
}
}
}
rm2(dryrun = TRUE)
rm2(verbose = TRUE)
Update Added envir
argument to rm
in (1). It was already in (2).
Update 2 Minor imrovements to (2).