I'm trying to read a file with fread, and select columns based on both index and column names. I can do this with dplyr but cannot with data.table. Just learning the latter. Any idea how to do it? Can't find the solution online or help file. A dummy example:
library(readr)
library(data.table)
# Create dummy data
DT <- data.table(ID = 1:50,
Code = sample(LETTERS[1:4], 50, replace = T),
State = rep(c("Alabama","Indiana","Texas","Nevada"), 50))
# Export to csv
write_csv(DT,"test.csv")
rm(DT)
# Import
DT <- fread("test.csv", select = "ID") # col name
DT <- fread("test.csv", select = c(2)) # col index
DT <- fread("test.csv", select = c("ID") | c(2)) # both = ERROR
DT <- fread("test.csv")[c("ID") | c(2)] # Error too (NOT IDEAL since loading all data anyway)
# Dplyr's approach
DT <- read_csv("test.csv", col_select = c("ID") | c(2)) # Works!
A slight adaptation of https://stackoverflow.com/a/62207245/3358272: