Search code examples
rdataframer-faq

Drop data frame columns by name


I have a number of columns that I would like to remove from a data frame. I know that we can delete them individually using something like:

df$x <- NULL

But I was hoping to do this with fewer commands.

Also, I know that I could drop columns using integer indexing like this:

df <- df[ -c(1, 3:6, 12) ]

But I am concerned that the relative position of my variables may change.

Given how powerful R is, I figured there might be a better way than dropping each column one by one.


Solution

  • There's also the subset command, useful if you know which columns you want:

    df <- data.frame(a = 1:10, b = 2:11, c = 3:12)
    df <- subset(df, select = c(a, c))
    

    UPDATED after comment by @hadley: To drop columns a,c you could do:

    df <- subset(df, select = -c(a, c))