Identicon
Identicon

Reputation: 129

Exporting multiple csv files from an object in R

I've 10 excel files that I need to read them first, then I want to export each one of them as .csv file. I use the following code to read them all:

mydir = setwd("~/Dropbox/AnalysisFolder")
myfiles = list.files(path=mydir, pattern="*.xlsx", full.names=TRUE)
myfiles

After reading the 10 excel file they're stored into one object in the environment, how can I write each one of them to .csv file, with keeping the name of the new .csv file as it is in the excel file? Thank you in advance,

Upvotes: 0

Views: 224

Answers (1)

Ronak Shah
Ronak Shah

Reputation: 389265

You can take help of lapply, read the excel file with read_excel. Change xlsx extension to csv and write the file with write.csv.

setwd("~/Dropbox/AnalysisFolder")
myfiles = list.files(path=mydir, pattern="*.xlsx", full.names=TRUE)

lapply(myfiles, function(x) {
  data <- data.frame(readxl::read_excel(x))
  write.csv(data, sub('xlsx$', 'csv', basename(x)), row.names = FALSE)
})

Upvotes: 1

Related Questions