GregdeLima
GregdeLima

Reputation: 404

Filter a List of Files by Date String

I have a list of files:

file_list <- c("C:\\Users\\uname\\files/DailyOpenOrders_NET_20160922.csv", 
"C:\\Users\\uname\\files/DailyOpenOrders_NET_20160923.csv", 
"C:\\Users\\uname\\files/DailyOpenOrders_NET_20160927.csv", 
"C:\\Users\\uname\\files/DailyOpenOrders_NET_20160928.csv", 
"C:\\Users\\uname\\files/DailyOpenOrders_NET_20160929.csv", 
"C:\\Users\\uname\\files/DailyOpenOrders_NET_20160930.csv"
)

And a parsed list of those file name's dates, filtered only for the dates that are mondays:

file_dates_mon <- structure(list(file_dates = c("20161003", "20161010", "20161017", "20161024", "20161031", "20161107"), weekday = c("Monday", "Monday", 
"Monday", "Monday", "Monday", "Monday")), .Names = c("file_dates", 
"weekday"), row.names = c(NA, 6L), class = "data.frame")

I attempted using str_detect(file_list,file_dates_mon$file_dates) to attempt to filter the file list down to the dates that are monday only. The intent is to pre filter the file list, rather than merge 361 files then filter by date after.

Is there a way to pre-filter the list based on the dates in file_dates_mon? file_list is the result of

dir<- choose.dir()
file_list<-list.files(dir,full.names = TRUE,pattern="DailyOpenOrders_NET_*")

Upvotes: 0

Views: 870

Answers (1)

missuse
missuse

Reputation: 19716

The problem here is that str_detect is vectorized over pattern or string. Not both at the same time.

some patterns:

filter_mon <- c("20160929", "20160927")

now check str_detect:

library(stringr)
str_detect(file_list, filter_mon)
#output
[1] FALSE FALSE FALSE FALSE  TRUE FALSE

just the first pattern is matched in all strings. One TRUE is returned.

One way around that is to run str_detect for each file_dates_mon$file_dates (or as in this example filter_mon) and then Reduce the logical vectors:

file_list[Reduce("|", lapply(filter_mon, function(x) str_detect(file_list, x)))]

#output
[1] "C:\\Users\\uname\\files/DailyOpenOrders_NET_20160927.csv" "C:\\Users\\uname\\files/DailyOpenOrders_NET_20160929.csv"

grepl is also an alternative

file_list[Reduce("|", lapply(filter_mon, function(x) grepl(x, file_list)))]

Upvotes: 3

Related Questions