Drop data frame columns by name

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

    I keep thinking there must be a better idiom, but for subtraction of columns by name, I tend to do the following:

    df <- data.frame(a=1:10, b=1:10, c=1:10, d=1:10)
    
    # return everything except a and c
    df <- df[,-match(c("a","c"),names(df))]
    df
    

提交回复
热议问题