Home > Software design >  R - write_parquet in loop
R - write_parquet in loop

Time:11-24

fwrite is looping over many .csv files in the working directory but when I write a .parquet it overwrites each time.

I have tried several approaches, basically I am trying to use file name I to keep the .csv file name as shown below without overwriting it.

rm(list = ls())
gc()

# Set up environment #
require("data.table")
require("arrow")

# Set directory to data, define files #
setwd("E:/TransferComplete/07/")

files <- list.files(pattern = "csv")

for (i in files){  setwd("E:/TransferComplete/07/")
loopStart <- Sys.time()
  
  bb <- fread(i,header = TRUE,sep = ",", data.table = FALSE, stringsAsFactors = FALSE,
                select = c("x","y","z"))
  gc()
  
  
  write_parquet(bb,
  'E:/P/i.parquet')
  
  
  loopEnd <- Sys.time()
  loopTime <- round(as.numeric(loopEnd) - as.numeric(loopStart), 0)
}

CodePudding user response:

You were very close in your question. When you're writing the .parquet, you need to separate the i when writing the file or the loop will keep writing a file called i.parquet.

write_parquet(bb,paste0('E:/P/',i,'.parquet'))

CodePudding user response:

Replace this

write_parquet(bb,
  'E:/P/i.parquet')

to this

write_parquet(bb,paste0('E:/P/',i,'.parquet'))
  • Related