从 R 中的 data.table 中删除多列
在本文中,我们将看到如何在 R 编程语言中从 data.table 中删除多个列。
创建 data.table 进行演示:
R
# load the data.table package
library("data.table")
# create a data.table with 4 columns
# they are id,name,age and address
data = data.table(id = c(1,2,3) ,
name = c("sravan","bobby","satwik"),
age = c(23,21,17),
address = c("kakumanu","ponnur","hyd"))
# display
data
R
# load the data.table package
library("data.table")
# create a data.table with 4 columns
# they are id,name,age and address
data = data.table(id = c(1,2,3),
name = c("sravan","bobby","satwik"),
age = c(23,21,17),
address = c("kakumanu","ponnur","hyd"))
# remove age , name and address columns
data[ ,':='(age = NULL, address = NULL, name=NULL)]
# display
data
R
# load the data.table package
library("data.table")
# create a data.table with 4 columns
# they are id,name,age and address
data = data.table(id = c(1,2,3),
name = c("sravan","bobby","satwik"),
age = c(23,21,17),
address = c("kakumanu","ponnur","hyd"))
# remove id column
data[ ,':='(id=NULL)]
# display
data
R
# load the data.table package
library("data.table")
# create a data.table with 4 columns
# they are id,name,age and address
data = data.table(id = c(1,2,3),
name = c("sravan","bobby","satwik"),
age = c(23,21,17),
address = c("kakumanu","ponnur","hyd"))
# remove all columns
data[ ,':='(id=NULL,age = NULL, address = NULL, name=NULL)]
# display
data
输出:
这里我们将使用索引运算符[] 删除多列
Syntax: data[ ,’:='(column1 = NULL, column2 = NULL, column n=NULL)]
where
- data is the input data.table
- column is the columns to be removed
- := is the operator to be loaded in the data.table
示例 1:从 data.table 中删除多列的 R 程序
电阻
# load the data.table package
library("data.table")
# create a data.table with 4 columns
# they are id,name,age and address
data = data.table(id = c(1,2,3),
name = c("sravan","bobby","satwik"),
age = c(23,21,17),
address = c("kakumanu","ponnur","hyd"))
# remove age , name and address columns
data[ ,':='(age = NULL, address = NULL, name=NULL)]
# display
data
输出:
示例 2:仅从 data.table 中删除一列
电阻
# load the data.table package
library("data.table")
# create a data.table with 4 columns
# they are id,name,age and address
data = data.table(id = c(1,2,3),
name = c("sravan","bobby","satwik"),
age = c(23,21,17),
address = c("kakumanu","ponnur","hyd"))
# remove id column
data[ ,':='(id=NULL)]
# display
data
输出:
示例 3: R 程序删除所有列
电阻
# load the data.table package
library("data.table")
# create a data.table with 4 columns
# they are id,name,age and address
data = data.table(id = c(1,2,3),
name = c("sravan","bobby","satwik"),
age = c(23,21,17),
address = c("kakumanu","ponnur","hyd"))
# remove all columns
data[ ,':='(id=NULL,age = NULL, address = NULL, name=NULL)]
# display
data
输出:
Null data.table (0 rows and 0 cols)