Load demo data
library(data.table) flights = fread("https://raw.githubusercontent.com/Rdatatable/data.table/master/vignettes/flights14.csv") flights
Select one single column
Suppose I want to select the first column year
.
flights[, 1] # return data.table
flights[, .(year)] # return data.table
flights[, c("year")] # return data.table
flights[, "year"] # return data.table
flights[, year] ### This would return a vector of the column. The same as flights[[year]] or flights$year
select_col = "year"; flights[, ..select_col]
select_col = "year"; flights[, select_col, with = F]
Select 2 or more columns
Suppose I want to select the first three columns: year
, month
, day
.
flights[, 1:3] # return data.table
flights[, c(1, 2, 3)] # return data.table
flights[, .(year:day)] ### This is to do math calculation: year ÷ day, cuz data.table will consider it as an expression.
flights[, year:day] # return data.table
flights[, .(year, month, day)] # return data.table
flights[, c("year", "month", "day")] # return data.table
select_col = c("year", "month", "day"); flights[, ..select_col] # return data.table
select_col = c("year", "month", "day"); flights[, select_col, with = F] # return data.table
# Invert selection columns
flights[, !(year:day)] # return data.table
flights[, !year:day] ### this will do another different math operation. cuz data.table will consider it as an expression.
- selecting referring column table manyselecting referring column table quot el-table-column selection column table-column el-table-column ant-design columns design table a-table columns typeof keyof 数量tables 1213 many quot querymultiple selected columns el-table-column数据column table table-column formatter多个 格式