5

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 ...
Gina Roussos
  • 53
  • 1
  • 1
  • 5
  • 4
    You are trying to remove a column from a data set? Don't use `remove`. It's more appropriate to do `test2$verbcatTC <- NULL` – Rich Scriven Oct 24 '18 at 16:09
  • 1
    If you mean removing an object from the environment, then use the function `rm()`. If you want to remove a particular column, then as @Rich Scriven mentioned, you `df$col <- NULL` – SmitM Oct 24 '18 at 16:10
  • It is also *possible* to do `within(test2, rm(verbcatTC))` – s_baldur Oct 24 '18 at 16:25

1 Answers1

7

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.

Duke Showbiz
  • 252
  • 1
  • 11
  • Thank you so much everyone! My apologies for posting a duplicate question; it seems I wasn't using the right lingo in my search terms (e.g., "variable" instead of "column"). I really appreciate the clarification that `remove()` is for removing *objects* and columns (aka variables) are *not* objects. So excited to set a bunch of crap variables to `NULL` so they are out of my dataset, lol. – Gina Roussos Oct 24 '18 at 17:03