Ditlev Reventlow
Ditlev Reventlow

Reputation: 47

Importing files with almost similar path and name

I have many txt files that I want to import into R. These files are imported one by one, I do the operations that I want, and then I import the next file.

All these files are located in a database system where all the folders have almost the same names, e.g.

database\type4\system50  

database\type6\system50 

database\type4\system30 

database\type4\system50 

Similarly, the names of the files are also almost the same, referring to the folder where they are positioned, e.g..

type4.system50.txt

type6.system50.txt

type4.system30.txt

type4.system50.txt

I have heard that there should be a easier way of importing these many files one by one, than simply multiple setwd and read.csv2 commands. As far as I understand this is possible by the macro import function in SAS, where you specify an overall path and then for each time you want to import a file you specify what is specific about this file name/folder name.

Is there a similar function in R? I tried to look at Importing Data in R like SAS macro

, but this question did not really show me how to specify the folder name/file name.

Thank you for your help.

Upvotes: 0

Views: 511

Answers (2)

TC Zhang
TC Zhang

Reputation: 2797

If you want to specify folder name / file name, try this

    databasepath="path/to/database"

    ## list all files
    list.files(getwd(),recursive = T,full.names = T,include.dirs = T) -> tmp

    ## filter files you want to read
    readmyfile <- function(foldername,filename){
      tmp[which(grepl(foldername,tmp) & grepl(filename,tmp))]
    }
    files_to_read <- readmyfile("type4", "system50")

    some_files <- lapply(files_to_read, read.csv2)

    ## Or you can read all of them (if memory is large enough to hold them)

    all_files <- lapply(tmp,read.csv2)

Upvotes: 1

dbk
dbk

Reputation: 61

Instead of using setwd continuously, you could specify the absolute path for each file, save all of the paths to a vector, loop through the vector of paths and load the files into a list

library(data.table)

file_dir <- "path/to/files/"

file_vec <- list.files(path = file_dir, pattern = "*.txt")

file_list <- list()

for (n in 1:length(file_list)){
  file_list[[n]] <- fread(input = paste0(file_dir, file_vec[n]))
}

Upvotes: 1

Related Questions