How To Delete Objects in R

How To Delete Objects in R
When we work in R, we have to create many objects. Sometimes we create objects for temporary purposes. In some certain situation, those temporary objects become huge and they many allocate so much memory in our system. It is better to clean workspace in R by removing objects those will not use no more to make our system faster.

To see the stored objects we can use the following command

ls()
1
[1] "first_name" "last_name"  "new_df"     "players"    "score"     

The ls() function returns a vector listing lists all the objects (vectors, data frames, etc) in your current workspace.

There are a few ways to remove data frames and other objects from your workspace:

Method-1:

# Remove these three objects
> rm("first_name", "last_name", "new_df")

Method-2:

# Or remove objects listed in a vector
> rm(list = c("first_name", "last_name", "new_df"))

#remove all
rm(list=ls(all=TRUE))

# Or remove all files from your workspace
> rm(list = ls())

Method-3:

# Or remove vectors programmatically. 
# Delete objects with underscore in name
> rm(list = ls()[grepl("_", ls())])

Add a Comment