Search code examples
rfor-loopdata.tablesapply

Apply a function to a list of csv files


I have 45 csv files in a folder called myFolder. Each csv file has 13 columns and 640 rows.

I want to read each csv and divide the columns 7:12 by 10 and save it in a new folder called 'my folder'. Here's my appraoch which is using the simple for loop.

library(data.table)
dir.create('newFolder')

allFiles <- list.files(file.path('myFolder'), pattern = '.csv')

for(a in seq_along(allFiles)){

    fileRef <- allFiles[a]
    temp <- fread(file.path('myFolder', fileRef)
    temp[, 7:12] <- temp[, 7:12]/10
    fwrite(temp, file.path('myFolder', paste0('new_',fileRef)))
 }

Is there a more simple solution in a line or two using datatable and apply function to achieve this?


Solution

  • Your code is already pretty good but these improvements could be made:

    • define the input and output folders up front for modularity
    • use full.names = TRUE so that allFiles contains complete paths
    • use .csv$ as the pattern to anchor it to the end of the filename
    • iterate over the full names rather than an index
    • use basename in fwrite to extract out the base name from the path name

    The code is then

    library(data.table)
    
    myFolder <- "myFolder"
    newFolder <- "newFolder"
    
    dir.create(newFolder)
    allFiles <- list.files(myFolder, pattern = '.csv$', full.names = TRUE)
    
    for(f in allFiles) {
        temp <- fread(f)
        temp[, 7:12] <- temp[, 7:12] / 10
        fwrite(temp, file.path(newFolder, paste0('new_', basename(f))))
    }