Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Removing NULL objects from an environment

Tags:

r

quantmod

I have an environment of objects that are either xts or NULL. I would like to remove all the NULL from the environment. Is there a function I can use with eapply to achieve this?

like image 528
lab_notes Avatar asked Oct 26 '12 21:10

lab_notes


People also ask

How do you remove an object from the environment?

remove and rm can be used to remove objects. These can be specified successively as character strings, or in the character vector list , or through a combination of both. All objects thus specified will be removed. If envir is NULL then the the currently active environment is searched first.

How do I remove something from an environment in R?

When you want to clear a single variable from the R environment you can use the “rm()” command followed by the variable you want to remove. variable: that variable name you want to remove.

How do I remove nulls in R?

If a list contains NULL then we might want to replace it with another value or remove it from the list if we do not have any replacement for it. To remove the NULL value from a list, we can use the negation of sapply with is. NULL.


1 Answers

rm(
  list=names(
    which(
      sapply(globalenv(),is.null) # or .GlobalEnv
      )
    )
  )

If it's not the global environment, you can use the envir switch in rm and wrap the environment name in getenv() in the sapply call

like image 78
Brandon Bertelsen Avatar answered Sep 19 '22 03:09

Brandon Bertelsen