我想从数据帧中删除一些列。我知道我们可以使用如下方法单独删除它们:

df$x <- NULL

但我希望用更少的命令来做到这一点。

另外,我知道我可以像这样使用整数索引删除列:

df <- df[ -c(1, 3:6, 12) ]

但我担心变量的相对位置可能会改变。

考虑到R的强大功能,我认为可能有一种比逐个删除每一列更好的方法。


当前回答

提供数据帧和一串逗号分隔的名称来删除:

remove_features <- function(df, features) {
  rem_vec <- unlist(strsplit(features, ', '))
  res <- df[,!(names(df) %in% rem_vec)]
  return(res)
}

用法:

remove_features(iris, "Sepal.Length, Petal.Width")

其他回答

还有一个子集命令,如果你知道你想要哪些列,它很有用:

df <- data.frame(a = 1:10, b = 2:11, c = 3:12)
df <- subset(df, select = c(a, c))

要删除列a,c,你可以这样做:

df <- subset(df, select = -c(a, c))
within(df, rm(x))

可能是最简单的,或者对于多个变量:

within(df, rm(x, y))

或者如果你在处理数据。如何在data.table中按名称删除列?

dt[, x := NULL]   # Deletes column x by reference instantly.

dt[, !"x"]   # Selects all but x into a new data.table.

或者对于多个变量

dt[, c("x","y") := NULL]

dt[, !c("x", "y")]

使用which找到要删除的列的索引。给这些下标一个负号(*-1)。然后对这些值进行子集化,这将从数据框架中删除它们。这是一个例子。

DF <- data.frame(one=c('a','b'), two=c('c', 'd'), three=c('e', 'f'), four=c('g', 'h'))
DF
#  one two three four
#1   a   d     f    i
#2   b   e     g    j

DF[which(names(DF) %in% c('two','three')) *-1]
#  one four
#1   a    g
#2   b    h
DF <- data.frame(
  x=1:10,
  y=10:1,
  z=rep(5,10),
  a=11:20
)
DF

输出:

    x  y z  a
1   1 10 5 11
2   2  9 5 12
3   3  8 5 13
4   4  7 5 14
5   5  6 5 15
6   6  5 5 16
7   7  4 5 17
8   8  3 5 18
9   9  2 5 19
10 10  1 5 20

DF[c("a","x")] <- list(NULL)

输出:

        y z
    1  10 5
    2   9 5
    3   8 5
    4   7 5
    5   6 5
    6   5 5
    7   4 5
    8   3 5    
    9   2 5
    10  1 5

在Bernd Bischl的BBmisc包中有一个名为dropNamed()的函数就是这样做的。

BBmisc::dropNamed(df, "x")

优点是它避免了重复数据帧参数,因此适合在magrittr中管道(就像dplyr方法一样):

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