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.
标签:return,referring,ways,selecting,flights,year,table,data,select
From: https://www.cnblogs.com/res-daqian-lu/p/17480213.html