Drop data frame columns by name

后端 未结 20 2578
花落未央
花落未央 2020-11-22 01:06

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
<         


        
20条回答
  •  遇见更好的自我
    2020-11-22 01:55

    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))
    

提交回复
热议问题