Cannot use remove() to delete a variable in r [duplicate]

I have successfully used the remove() function in the past to remove both datasets and variables. However, over the past few days I have been unable to remove variables, although I am able to remove datasets.

I don't know how to upload data to a stackoverflow question, but I can show you the code I used and the output I've gotten. The dataset is called test2 and the variable is verbcatTC. I do not have the dataset attached. Below are four attempts to remove this variable, along with the warning message received each time. Then I run two lines of code to show you all that this variable exists in the dataset.

Thank you so much for your help and let me know if I can provide further information. Also, what other function could I use to get rid of variables in case I can't get remove() to work?

> remove(test2$verbcatTC)
Error in remove(test2$verbcatTC) : ... must contain names or character strings
> remove("verbcatTC")
Warning message:
In remove("verbcatTC") : object 'verbcatTC' not found
> remove(verbcatTC)
Warning message:
In remove(verbcatTC) : object 'verbcatTC' not found
> remove("test2$verbcatTC")
Warning message:
In remove("test2$verbcatTC") : object 'test2$verbcatTC' not found
> head(test2$verbcatTC)
[1] Positive Positive Positive Positive Positive Positive
Levels: Negative Positive
> str(test2$verbcatTC)
Factor w/ 2 levels "Negative","Positive": 2 2 2 2 2 2 2 2 2 2 ...
3

1 Answer

rm() and remove() are for removing objects in your an environment (specifically defaults the global env top right of the RStudio windows), not for removing columns. You should be setting cols to NULL to remove them, or subset (or Dplyr::select etc) your dataframe to not include the columns you want removed. If you just want the whole dataframe removed from an environment, then rm() still works.

1

You Might Also Like