remove column 'a' and 'c' from a df in r

Here are two ways to remove columns 'a' and 'c' from a data frame df in R:

  1. Using base R:
main.r
df <- df[, !(names(df) %in% c('a', 'c'))]
42 chars
2 lines

This code subsets df by selecting all columns except those whose names are 'a' or 'c'.

  1. Using the select() function from the dplyr package:
main.r
library(dplyr)
df <- select(df, -a, -c)
40 chars
3 lines

This code uses the select() function to select all columns from df except those named 'a' or 'c'. The - before column names means to exclude them.

Either way, df will be modified to contain all columns except 'a' and 'c'.

gistlibby LogSnag