rm () function not working

Hallo everybody,

I am stucked in a vary basic tool.. rm()!

Want to erase some variables from my dataframe,
and I call

x<- c("data$var1", " data$var2")

then I call


and it is still there!

Anybody can help?

Running x <- c("data$var1", " data$var2") creates an object called x that contains the two listed character strings. x is a new object that has nothing to do with the data frame data or any columns within data. "data$var1" and "data$var2" are just character strings. They don't reference the data frame data or any of its columns, which you would reference with data$var1 or data$var2 (without the quotation marks). Running rm(x) just removes x (the vector containing the two character strings) and does not affect the data frame data at all.

If you want to remove the columns var1 and var2 from data you could do the following:

data = data[ , !(names(data) %in% c("var1", "var2"))]

To see what the %in% function is doing, trying running the following:

names(data) %in% c("var1", "var2")

!(names(data) %in% c("var1", "var2"))

names(data)[!(names(data) %in% c("var1", "var2"))]

To remove columns, you can also do the following:


data = data %>% select(-var1, -var2)

rm() cannot be used to remove variables from a data frame.
To remove a variable, you need to set it to NULL:

data$var1 <- NULL



data <- data %>% mutate(var1 = NULL, var2 = NULL)

Setting it to NULL avoids making a copy of the original data frame.

thanks so much for your help!

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.