Drop data frame columns by name

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

    Out of interest, this flags up one of R's weird multiple syntax inconsistencies. For example given a two-column data frame:

    df <- data.frame(x=1, y=2)
    

    This gives a data frame

    subset(df, select=-y)
    

    but this gives a vector

    df[,-2]
    

    This is all explained in ?[ but it's not exactly expected behaviour. Well at least not to me...

提交回复
热议问题