Bigchao
Bigchao

Reputation: 1756

Selectively Fread based on the results from data.table

I have a folder full of csv files with certain naming pattern (all with 4 digits), "0001.csv", "0002.csv" etc. All the files have same headings and format.

If I want to combine the ALL the files or do analysis with them. I can do this

all.files <- list.files(path = "/Users",pattern = ".csv")
DT <- lapply(all.files, fread # or other self-defined function)
DT <- rbindlist(DT)

BUT now I want to do the list.filesbased on certain output from one data.table.

dt <- data.table(Ticker = c("0001","0002","0003","0004"), Status = c("True", "True","True","False"))
> dt
   Ticker Status
1:   0001   True
2:   0002   True
3:   0003   True
4:   0004  False

I want to combine the files whose status is TRUE in dt. Not all. so list.files may not be applicable.

Any suggestions? thanks a lot.

Upvotes: 0

Views: 96

Answers (1)

LyzandeR
LyzandeR

Reputation: 37879

If I get it correctly this can be done as:

DT <- lapply(paste0(dt[Status == 'True', Ticker], '.csv'), function(x) fread(x))
DT <- rbindlist(DT)

Upvotes: 2

Related Questions