R - How to read multiple files from a folder, convert them in xts and do some data analysis on them?

六眼飞鱼酱① 提交于 2019-12-08 12:59:45

问题


I have a folder with 199 files (from blah-001-a.exp to blah-199-a.exp)

I want to do something like this for every file:

DF<- read.csv("D:/ebook/myfolder/blah-001-a.exp", sep=";")
xts<-xts(x=DF[,-c(1,13)], order.by = as.Date(x=DF$DATA,format="%d.%m.%Y"))
#other codes and report pdf file...

I tried some code like this but it only reads the files without trasform them into xts:

folder <- "D:/ebook/myfolder/"    
filenames <- list.files(path=folder) 
for (i in 1:length(filenames)){
      assign(filenames[i], 
             read.csv(paste0(folder, filenames[i]),sep=';')
      )}

Could you give me some suggestions? Thanks in advance.


回答1:


do.stuff <- function(file_in){
    DF <- read.csv(file_in, sep=";")
    xts <-xts(x=DF[,-c(1,13)], order.by =as.Date(x=DF$DATA,format="%d.%m.%Y"))
}
the_files <- list.files('path/to/files',full.names = T)
the_stuff <- lapply(1:length(the_files),function(i)do.stuff(the_files[[i]]))
names(the_stuff) <- basename(the_files)


来源:https://stackoverflow.com/questions/36894446/r-how-to-read-multiple-files-from-a-folder-convert-them-in-xts-and-do-some-da

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!