Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in R Programming by (7.3k points)

I have loaded in an R console different type of objects. I can remove them all using

rm(list=ls())

or remove only the functions (but not the variables) using

rm(list=lsf.str())

My question is: is there a way to remove all variables except the functions

1 Answer

0 votes
by

In this case, you can use the setdiff function to remove all variables except functions as follows:

rm(list = setdiff(ls(), lsf.str())) 

ls() returns the set of objects in the global environment.

lsf.str() returns the set of objects having mode function.

And,

setdiff() is used to find the subset of objects in the global environment not having mode as function.

Browse Categories

...