Select Subset of Columns based on Vector R

Thanks everyone for answering so quickly. A couple of your solutions worked. I realized the biggest problem I had was that I didn't a true vector f! And once I did, it was still being treated as a factor! So I thought I would post what I did with your help to fix it.

f <- select(filter(hash_t,(variable %in% c("a",...,"z"))),X) #wasn't a vector...
f[,1]<-sapply(f[,1],as.character) # had to remove factor info from f; still not a vector...
f<-f[,1] # Now I had a vector!
x1<-x[,!names(x) %in% f]  #worked!

Many of the thoughts posted worked. I just had a problem with my selection criteria not being a proper vector and having some factor issues associated with it.


Use %in%:

names.use <- names(df)[!(names(df) %in% f)]

Then names.use will contain the names of all the columns which are not contained in your vector of names f.

To subset your data frame using the columns you want, you can use the following:

df.subset <- df[, names.use]

Here is a data.table solution with a reproducible example.

# load library
  library(datat.table)

# get data
  data(iris)

# convert your dataset into data.table
  setDT(iris)

# vector of columns you DON'T want
  f <- c("Sepal.Width","Petal.Length")


# subset
  output <- iris[, !f, with = FALSE]

You can also do:

subset(x, select=f)

Unlike using %in%, this will throw an error if one of the values in f is not a column name in x, which can be helpful for spotting typos if you've typed out some of them manually.

Tags:

R