r - 从文件夹中加载多个文件作为循环

标签 r

我在一个文件夹中有多个文件,我想将它们加载到 R 中并进行操作,然后另存为 data.table。有没有办法通过定义要加载的文件来自动化输入和输出?

文件夹中的文件:

data1.xlsx
data2.xlsx
data3.xlsx

我的脚本:
data1.d = read.xls("data1.xlsx", sheet = 5, header = TRUE)
data1.d$Date <- format(as.Date(data1.d$Date, format = "%Y-%m-%d"), "%m/%d/%Y")
data1.d$Time <- "17:00:00"
data1.d$OI <- "1"
write.table(data1.d, file="data1.Ready.txt", col.names = T, quote = F,sep=",",row.names = F)

最佳答案

我们可以使用 list.files 从目录中获取文件列表或 dir ,指定 pattern如有必要,循环遍历 filesimap , 用 read_excel 读取 excel 文件( readxl 包),使用 mutate 创建列,然后使用 write.table 将其写回新文件通过替换 .xlsx 更改文件名后( str_remove ) 并将末尾的“.Ready.txt”与 str_c 连接起来

files <- list.files(pattern = '^data\\d+\\.xlsx$')
library(readxl)
library(purrr)
library(stringr)
imap(set_names(files, files), ~ read_excel(.x, sheet = 5) %>%             
             mutate(Date = format(ymd(Date), "%m/%d/%Y"),
             Time = "17:00:00", OI = "1") %>%
            select(1,8,2,3,4,5,6,7) %>%
            set_names(c("Date","Time","Open","High","Low","Close","Vol","OI")) %>%
             write.table(file = str_c(str_remove(.y, "\\.xlsx"), 
        ".Ready.txt"), col.names = TRUE, quote = FASLE, sep=",", 
         row.names = FALSE))

关于r - 从文件夹中加载多个文件作为循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62269566/

相关文章:

r - R 中的 "Only the first element of list is used"

json - 在R中读取json文件:词法错误:json文本中的字符无效

r - 理解为什么线性回归没有按预期处理我的分类变量?

r - 嵌套函数是否更慢?

r - 派对工具包 : Change terminal node boxplots to bar graphs that shows mean and standard deviation

r - 控制 lme4 1.0.* 中的最大迭代次数

r - 函数将 n 个整数除以 app.大小相同

r - 计算数据框中每个第二个值的平均值

R矩阵获得两个最高值的索引

r - 在数据框中查找与 R 中最接近的匹配行