loop through batch read folders in setwd(), format dfs & write.csv() to different folder R

64 Views Asked by At

I have folders with 4 .csv files in each folder. Currently I am batch reading the .csv files in the folder:

setwd("/Users/Drive/MS/Ma/Ec/Effort_variation_Ec/MES1/")
ecosmpr <-
  list.files(pattern = "*.csv") %>% 
  map_df(~read_csv(.))
ecosmpr=data.frame(ecosmpr)

After batch reading in the 4 csvs in the folder as one data.frame, I need to do some formatting:

ecosmpr1=ecosmpr[,-c(2:13)]
dim(ecosmpr1)

ecosmpr1=ecosmpr1 %>%
  row_to_names(row_number = 1)

names(ecosmpr1)=rev(c("detritus","phyto","peri","zoops","amphipods","inverts","leucisids","lns","yct5plus","yct4","yct3","yct2","yctyoy","lkt5plus","lkt34","lkt2","lkt7mo1yo",
                      "lktyoy","years"))

Then I want to export the formatted data.frame to a csv, but in a different location:

write.csv(ecosmpr1,"/Users/Drive/MS/Ma/Ec/Effort_variation_Ec/ecosmpr1_partialformat.csv",row.names = FALSE)

My issue is that I need to loop through the first setwd(MESXX) rename each "ecosmprXX" and export each "ecosmprXX_partialformat.csv" I am having issues with even starting this loop. My naming convention for the folder is MESXX (where XX is the number, 1:30),data frame is ecosmprXX (where XX is the number, 1:30), and exported .csv is ecosmprXX_partialformat.csv (where XX is the number, 1:30). I have 30 different folders so doing this without a loop is inefficient.

1

There are 1 best solutions below

0
On BEST ANSWER

This should do the trick:

library(tidyverse)
library(janitor)


new_col_names <- rev(c("detritus","phyto","peri","zoops","amphipods","inverts","leucisids","lns",
                       "yct5plus","yct4","yct3","yct2","yctyoy","lkt5plus","lkt34","lkt2",
                       "lkt7mo1yo", "lktyoy","years"))

for (i in 1:30) {
  
  setwd(paste0("/Users/Drive/MS/Ma/Ec/Effort_variation_Ec/MES", i, "/"))
  
  ecosmpr <- list.files(pattern = "*.csv") %>% 
    map_df(~read_csv(.x))
  
  ecosmpr <- ecosmpr %>% 
    select(-c(2:13)) %>% 
    row_to_names(row_number = 1)
  
  names(ecosmpr) <- new_col_names
  
  output_file <- 
    paste0("/Users/Drive/MS/Ma/Ec/Effort_variation_Ec/ecosmpr", i, "_partialformat.csv")
  
  write.csv(ecosmpr, output_file, row.names = FALSE)
}