[英]Exporting multiple csv files from an object in R
我有 10 個 excel 文件,我需要先閱讀它們,然后我想將它們中的每一個導出為 .csv 文件。 我使用下面的代碼來閱讀它們:
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? 先感謝您,
您可以借助lapply
,使用read_excel
閱讀 excel 文件。 將xlsx
擴展名更改為csv
並使用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)
})
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.