Rohan Bali
Rohan Bali

Reputation: 119

Read multiple files from a folder and pass each file through a function in R

    filenames <- list.files("foldername", pattern="*.xlsx", full.names=TRUE)
for(file in filenames){

      data_preprocessed <- file %>% 
        group_by(date = floor_date(DATE,"month")) %>%
        summarize(SALES = sum(SALES)) %>%
        separate(date, sep="-", into = c("year", "month")) %>%
        mutate(lag_12 = shift(SALES,-12),
               lag_24 = shift(SALES,-24)) %>% 
        rowwise()
      write_xlsx(data_preprocessed,"file_name.xlsx")
    }

All files should be read one after the other and then saved/written(write.xlsx) with a different name.

Upvotes: 0

Views: 492

Answers (1)

Ronak Shah
Ronak Shah

Reputation: 388807

You can write a function which

1) Reads the file

2) Performs all the data-processing steps

3) writes the new file

library(tidyverse)
library(lubridate)
library(data.table)

f1 <- function(file) {
   readxl::read_xlsx(file) %>%
      group_by(date = floor_date(DATE,"month")) %>%
      summarize(SALES = sum(SALES)) %>%
      separate(date, sep="-", into = c("year", "month")) %>%
      mutate(lag_12 = shift(SALES,-12),
             lag_24 = shift(SALES,-24))  %>%
      writexl::write_xlsx(paste0('new_', basename(file)))    
}

and do this for every file.

lapply(filenames, f1)

Upvotes: 1

Related Questions