In this article we will work on learning how to remove data frame in R using remove() command.


Theory

It is often the case, when importing data into R, that we have more than one or two data frames with raw data.

Then we figure out the variables we need, and do the merging (for example, we do inner merge of the data frames).

Now we have the table we will be working with for the analysis part.

But we also have the R Studio environment filled with the raw data frames we already don’t need.

To clean up the environment/workspace in R Studio, we will need to remove the extra data frames we won’t be using further.



Application

Below are the steps we are going to take to make sure we do master the skill of removing data frames in R:

  1. Basic remove() command description
  2. Creating a sample table in R
  3. Removing data frame in R



Part 1. Basic remove() command description

The short theoretical explanation of the function is the following:

remove(object1, object2, ...)

Here, “object” refers to either a table, or a data frame, or any other data structure you would like to remove from the environment in R Studio.



Part 2. Creating a sample table in R

For the purpose of this article I will create one data frame (table) with two variables: “id” and “name”.

Note: it doesn’t matter what you will fill your data frame with, because in this article what we are going to learn is how to remove it from the R Studio environment. You can even create a blank data frame and it will work the same way 🙂

You can create these sample data frame using the following code:


mydata<-data.frame(id = c("11", "12"),
name = c("John Smith", "Erik Dawson"))

We have just created a 2x2 data frame and we can take a look at it using the following code:


View(mydata)

Note: in this article I create my own dataset. If you have your own in a csv or excel files, you can follow the same procedure to arrive at the result.



Part 3. Removing data frame in R

So we created the sample table and (assume) we gathered all the information and statistics we need for further analysis.

We don't want to overcrowd the R Studio environment, so we want to remove this data frame.

Note: remember that this is absolutely not required and I personally remove data frames if I don't need to use them for sure and/or when their count exceeds 6-8 just so it's not too many objects in R Studio.

At this point, I'm sure that you are sure that you need to remove the data frame in R from your R Studio environment!

You can do it using the following code:


remove(mydata)

All done! Congratulations! We removed the data frame in R from our R Studio environment and you won't see it in the right top corner under the "Environment" section.



If you liked this article, I encourage you to take a look at the Data Manipulation in R section where you will find a lot of useful information and master the skill of data wrangling.