How many ways of selecting/referring to a column in data.table?

发布时间 2023-06-14 14:53:10作者: DaqianLU

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.