Drop data frame columns by name

后端 未结 20 2582
花落未央
花落未央 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:57

    Another possibility:

    df <- df[, setdiff(names(df), c("a", "c"))]
    

    or

    df <- df[, grep('^(a|c)$', names(df), invert=TRUE)]
    
    0 讨论(0)
  • 2020-11-22 01:57

    Another solution if you don't want to use @hadley's above: If "COLUMN_NAME" is the name of the column you want to drop:

    df[,-which(names(df) == "COLUMN_NAME")]
    
    0 讨论(0)
提交回复
热议问题