df$x <- NULL
df <- df[ -c(1, 3:6, 12) ]
within(df, rm(x))
is probably easiest, or for multiple variables:
within(df, rm(x, y))
Or if you're dealing with data.table
s (per How do you delete a column by name in data.table?):
dt[, x := NULL] # Deletes column x by reference instantly.
dt[, !"x"] # Selects all but x into a new data.table.
or for multiple variables
dt[, c("x","y") := NULL]
dt[, !c("x", "y")]
df <- data.frame(a = 1:10, b = 2:11, c = 3:12)
df <- subset(df, select = c(a, c))
df <- subset(df, select = -c(a, c))
df[, !(colnames(df) %in% c("x","bar","foo"))]
import pandas as pd
# Create a dataframe from a dict
df = pd.DataFrame({"a": [1,2,3], "b":[2,4,6]})
print("The DataFrame object before deleting the column")
print(df)
df.drop('a', inplace=True, axis=1)
print("The DataFrame object after deleting the column a")
print(df)
DF <- data.frame(
x=1:10,
y=10:1,
z=rep(5,10),
a=11:20
)
drops <- c("x","z")
DF[ , !(names(DF) %in% drops)]
Or, alternatively, you can make a list of those to keep and refer to them by name :
keeps <- c("y", "a")
DF[keeps]
EDIT : For those still not acquainted with the drop argument of the indexing function, if you want to keep one column as a data frame, you do:
keeps <- "y"
DF[ , keeps, drop = FALSE]
drop=TRUE (or not mentioning it) will drop unnecessary dimensions, and hence return a vector with the values of column y.