How remove all tibble dataframe?

Viewed 24

I would like transform this line code :

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

to apply for all data.frame and tbl_df

thanks in advance.

1 Answers

Kind of ugly, but I think this works.

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

When you do class(my_tibble) it gives you c("tbl_df", "tbl", "data.frame"). So the output you get from the first sapply() above gives you a list of vectors, which you can sapply() over a second time.

Although it may be easier to just restart your R session.

Related