Home > database >  How to remove all "xts" "zoo" object from R environment?
How to remove all "xts" "zoo" object from R environment?

Time:08-19

I need help on removing all "xts" "zoo" object from R (global)environment.

I learned that I can remove dataframes by using the following:

rm(list=ls(all=TRUE)[sapply(mget(ls(all=TRUE)), class) == "data.frame"])

The object I am trying to remove from environment is called temp. Running the following: class(temp) yields [1] "xts" "zoo".

So I modified the code for deleting data frames like so: rm(list=ls(all=TRUE)[sapply(mget(ls(all=TRUE)), class) == "xts"]) or rm(list=ls(all=TRUE)[sapply(mget(ls(all=TRUE)), class) == "zoo"]) and other combinations of the "xts" and/or "zoo" but none of them works.

The temp object was created using temp <- as.xts(as.data.table(data))

Any suggestions?

CodePudding user response:

  • You may try
rm(list=ls(all=TRUE)[sapply(mget(ls(all=TRUE)), \(x) class(x)[[1]]) == "xts"])

CodePudding user response:

With Filter:

rm(list = Filter(function(x) 'xts' %in% class(get(x)), ls(all = TRUE)))
  • Related