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

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))

Dplyr解决方案

我怀疑这在这里会得到很多关注,但如果你有一个列列表,你想要删除,并且你想在dplyr链中做它,我在select子句中使用one_of():

这里有一个简单的,可复制的例子:

undesired <- c('mpg', 'cyl', 'hp')

mtcars <- mtcars %>%
  select(-one_of(undesired))

可以通过运行?one_of或在这里找到文档:

http://genomicsclass.github.io/book/pages/dplyr_tutorial.html

在数据帧中按列名删除和删除列。

A <- df[ , c("Name","Name1","Name2","Name3")]

另一个数据。还没有发布的表选项使用了特殊的动词。sd,代表数据的子集。与. sdcols参数一起,您可以按名称或索引选择/删除列。

require(data.table)
# data
dt = data.table(
  A = LETTERS[1:5],
  B = 1:5,
  C = rep(TRUE, 5)
)
# delete B
dt[ , .SD, .SDcols =! 'B' ]
# delete all matches (i.e. all columns)
cols = grep('[A-Z]+', names(dt), value = TRUE)
dt[ , .SD, .SDcols =! cols ]

数据中此类任务的所有选项的摘要。表格可以在这里找到

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")]