如何在R语言的data.table中通过列名删除列?


我们可以通过将列设置为空值来实现

示例

> library(data.table)
> df <- data.frame(numbers = 1:10, x = runif(10,25,75))
> data_table <- data.table(df)

要删除一列x

> data_table[, x:=NULL]
> data_table
numbers
1: 1
2: 2
3: 3
4: 4
5: 5
6: 6
7: 7
8: 8
9: 9
10: 10

要删除两列

> df <- data.frame(numbers = 0:9, x = runif(10,25,75), y=rnorm(10))
> Data_table <- data.table(df)
Data_table[, c("x","y"):=NULL]
> Data_table
numbers
1: 0
2: 1
3: 2
4: 3
5: 4
6: 5
7: 6
8: 7
9: 8
10: 9

更新于:2020-07-06

1K+浏览

开启你的职业生涯

参加课程即可获得认证

开始
广告
© . All rights reserved.