Drop data frame columns by name

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

    There's a function called dropNamed() in Bernd Bischl's BBmisc package that does exactly this.

    BBmisc::dropNamed(df, "x")
    

    The advantage is that it avoids repeating the data frame argument and thus is suitable for piping in magrittr (just like the dplyr approaches):

    df %>% BBmisc::dropNamed("x")
    

提交回复
热议问题