Syntax error How to delete columns by their name in data.table in R?

How to delete columns by their name in data.table in R?



We can do this by setting the column to NULL

Example

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

To delete one column 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

To delete two columns

> 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
Updated on: 2020-07-06T14:50:15+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements