Bohnston
Bohnston

Reputation: 69

Loop to include different folders and patterns

I have this code very inefficient and would like to make it better. Each df is a list of files that match a certain pattern as described by term6.

Can someone show me the best method to make this is into efficient and easily understandable code? Would a loop be best?

term6 <- c("Casos de Malaria|MALARIA8|Casos según Entidades")
term7 <- c("Casos según Entidades|MALARIA8")
term8 <- c("Distrito Capital10|hasta")
term9 <- c("NA12")
term10 <- c("hasta la semana|NA12|NA16|Entidades Federales16")
term11 <- c("NA19|Cuadro|Malaria16")

df6 <- list.files(path = "J:\\GBD2017\\Venezuela\\xlsx\\2006", pattern = term6, recursive = FALSE, full.names = FALSE, ignore.case = TRUE)
df7 <- list.files(path = "J:\\GBD2017\\Venezuela\\xlsx\\2007", pattern = term7, recursive = FALSE, full.names = FALSE, ignore.case = TRUE)
df8 <- list.files(path = "J:\\GBD2017\\Venezuela\\xlsx\\2008", pattern = term8, recursive = FALSE, full.names = FALSE, ignore.case = TRUE)
df9 <- list.files(path = "J:\\GBD2017\\Venezuela\\xlsx\\2009", pattern = term9, recursive = FALSE, full.names = FALSE, ignore.case = TRUE)
df10 <- list.files(path = "J:\\GBD2017\\Venezuela\\xlsx\\2010", pattern = term10, recursive = FALSE, full.names = FALSE, ignore.case = TRUE)
df11 <- list.files(path = "J:\\GBD2017\\Venezuela\\xlsx\\2011", pattern = term11, recursive = FALSE, full.names = FALSE, ignore.case = TRUE)

Upvotes: 2

Views: 89

Answers (2)

hrbrmstr
hrbrmstr

Reputation: 78792

c(
  "Casos de Malaria|MALARIA8|Casos según Entidades",
  "Casos según Entidades|MALARIA8",
  "Distrito Capital10|hasta",
  "NA12",
  "hasta la semana|NA12|NA16|Entidades Federales16",
  "NA19|Cuadro|Malaria16"
) -> terms

c(
  "J:\\GBD2017\\Venezuela\\xlsx\\2006", 
  "J:\\GBD2017\\Venezuela\\xlsx\\2007", 
  "J:\\GBD2017\\Venezuela\\xlsx\\2008", 
  "J:\\GBD2017\\Venezuela\\xlsx\\2009", 
  "J:\\GBD2017\\Venezuela\\xlsx\\2010",
  "J:\\GBD2017\\Venezuela\\xlsx\\2011"
) -> paths

unlist(lapply(seq_along(paths), function(.i) {
  list.files(
    path = paths[.i], 
    pattern = terms[.i], 
    recursive = FALSE, 
    full.names = FALSE, # generally not a good idea; I always set this to TRUE
    ignore.case = TRUE
  )  
}), use.names = FALSE)

However, I'd recommend keeping the terms and paths together. That way you can have a TSV (tab-separated file vs comma) file with the path search metadata in it and also ensure the length equality constraint for each vector.

Its just as easy to use:

data.frame(
  term = c(
    "Casos de Malaria|MALARIA8|Casos según Entidades",
    "Casos según Entidades|MALARIA8",
    "Distrito Capital10|hasta",
    "NA12",
    "hasta la semana|NA12|NA16|Entidades Federales16",
    "NA19|Cuadro|Malaria16"
  ),
  path = c(
    "J:\\GBD2017\\Venezuela\\xlsx\\2006", 
    "J:\\GBD2017\\Venezuela\\xlsx\\2007", 
    "J:\\GBD2017\\Venezuela\\xlsx\\2008", 
    "J:\\GBD2017\\Venezuela\\xlsx\\2009", 
    "J:\\GBD2017\\Venezuela\\xlsx\\2010",
    "J:\\GBD2017\\Venezuela\\xlsx\\2011"
  ),
  stringsAsFactors = FALSE
)-> file_search_df

unlist(lapply(1:nrow(file_search_df), function(.i) {
  list.files(
    path = file_search_df$path[.i], 
    pattern = file_search_df$term[.i], 
    recursive = FALSE, 
    full.names = FALSE, # generally not a good idea; I always set this to TRUE
    ignore.case = TRUE
  )  
}), use.names = FALSE)

And, if you use the TSV approach, even more compact:

file_search_df <- read.csv("path-to-metadata-file.tsv", sep = "\t", stringsAsFactors=FALSE)

unlist(lapply(1:nrow(file_search_df), function(.i) {
  list.files(
    path = file_search_df$path[.i], 
    pattern = file_search_df$term[.i], 
    recursive = FALSE, 
    full.names = FALSE, # generally not a good idea; I always set this to TRUE
    ignore.case = TRUE
  )  
}), use.names = FALSE)

Upvotes: 2

gaut
gaut

Reputation: 5958

The following should tidy up your code quite a bit:

#myfilepaths <- paste0(rep(getwd(),6))
myfilepaths <-  c(paste0('J:\\GBD2017\\Venezuela\\xlsx\\',2006:2011))
allterms <- c("test","Casos según Entidades|MALARIA8","Distrito Capital10|hasta", "NA12", "hasta la semana|NA12|NA16|Entidades Federales16", "NA19|Cuadro|Malaria16")
length(myfilepaths)==length(allterms)
searchtree <- data.frame(myfilepaths, allterms, stringsAsFactors = F)
result <- apply(searchtree, 1, function(x) list.files(x["myfilepaths"], pattern=x["allterms"], recursive = FALSE, full.names = FALSE, ignore.case = TRUE))

The idea is to 1) generate your folder list by pasting the year range to the root, then to put all combinations of paths and patterns to search into a neat dataframe, and 2) to apply the list.files function to this df. This gives you a list of 6 elements.

> result
[[1]]
[1] "ctest.R"       "ExcelTest.csv" "test.csv"      "test.xls"     

[[2]]
character(0)

[[3]]
character(0)

[[4]]
character(0)

[[5]]
character(0)

[[6]]
character(0)

Or you could also use a loop like so, yielding exactly the same:

result <- list()
for(i in 1:length(allterms)) {
  result[[i]] <- list.files(searchtree[i,"myfilepaths"], pattern=searchtree[i,"allterms"], recursive = FALSE, full.names = FALSE, ignore.case = TRUE)
}
result

edit: if you have more than one pattern per folder, for example if you want to search the 2006 folder for both "test" and "Casos según Entidades|MALARIA8", you should create the searchtree data.frame like so:

searchtree <- data.frame(myfilepaths[c(1,1:6)], allterms[c(1:2,2:6)], stringsAsFactors = F)
colnames(searchtree) <- c("myfilepaths", "allterms")

The function will search any pattern that is on the same row as any folder.

searchtree
                     myfilepaths                                        allterms
1 J:\\GBD2017\\Venezuela\\xlsx\\2006                                            test
2 J:\\GBD2017\\Venezuela\\xlsx\\2006                  Casos según Entidades|MALARIA8
3 J:\\GBD2017\\Venezuela\\xlsx\\2007                  Casos según Entidades|MALARIA8
4 J:\\GBD2017\\Venezuela\\xlsx\\2008                        Distrito Capital10|hasta
5 J:\\GBD2017\\Venezuela\\xlsx\\2009                                            NA12
6 J:\\GBD2017\\Venezuela\\xlsx\\2010 hasta la semana|NA12|NA16|Entidades Federales16
7 J:\\GBD2017\\Venezuela\\xlsx\\2011                           NA19|Cuadro|Malaria16

Or you could manage it manually:

allfolders <- c("folder1","folder1", "folder2")
allpatterns <- c("pattern1", "pattern2", "pattern2")
searchtree <- data.frame(myfilepaths, allterms, stringsAsFactors = F)
searchtree

  myfilepaths allterms
1     folder1 pattern1
2     folder1 pattern2
3     folder2 pattern2

Upvotes: 0

Related Questions