
NOTE: This is an instance in R when you don’t need to put the name of the variable in quotes (e.g., (“OCC”)) nor do you need to indicate which dataset the variable is in (e.g., (GSS2010$OCC)) since the dataset is already referenced in the subset command. In effect, OCC is deleted but, to get there, you actually have to tell R to keep everything but that (pretty stupid, honestly). Thus, “ -(OCC)” tells R to select the entire dataframe except the variable OCC for the subset.

However, the “ –” before “ (OCC)” actually tells R to select all the other variables BUT not the OCC variable for the subset. In this case, we use the “ select =” command to tell R that we want it to select a specific variable.
#Command to clear environment in r how to
After the comma inside the parentheses is code to tell R how to select the subset. It’s a very useful function for selecting, for instance, all the men in a sample or all of the people who live in a certain region.Īfter the “subset” function, inside the parentheses, is the name of the dataset from which we are taking a subset, GSS2010. The “ subset” function tells R that you want to take part of an existing dataset. However, in this case I actually want to overwrite the dataset, so I’m actually naming the new dataset the same thing as the old dataset, which, effectively, overwrites the dataset, getting rid of the unwanted variables in the process. Typically, when I use the subset function, I do so to create a different dataset. Here’s the code: GSS2010 <- subset(GSS2010, select = -(OCC)) To completely remove a variable from a dataframe, you need to tell R to copy the dataframe minus the variable you want to delete. You can open that file in R and follow along.

This short tutorial will explain how to delete a variable (or multiple variables if needed).Īs with most of my R examples, I’m going to use the 2010 wave of the General Social Survey (R version here) to illustrate. At times, I just want to get rid of a variable in a dataset (’cause screw that variable). I regularly create variables while analyzing data and then find that I need to delete a variable I created.
