循环目录以获取Excel内容 [英] Loop over directory to get Excel content

查看:48
本文介绍了循环目录以获取Excel内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含Excel文件的目录:

I have a directory with Excel files:

sites=list.files(pattern='[.]xls')
> sites
[1] "test1.xls" "test2.xls" "test3.xls"

这有效:

a=read.xlsx(sites[1],14)

所以我希望这也会起作用:

So I would expect that this would work too:

df=data.frame()
  for (i in sites){
  x=read.xlsx(sites[i],14)
  x=x[560:831,12:14]
  df=rbind.fill(df,x)
  }

但是,这给出了:

Error in loadWorkbook(file) : Cannot find NA

这是怎么回事?另外,有没有一种方法可以对此进行矢量化处理-文件很大且加载速度很慢;由于数据的格式不正确,因此无法使用read.xlsx2.

What is going wrong here? Also, is there a way to vectorise this - the files are large and loading is slow; I can't use read.xlsx2 since the data are not in the right [tabular] format.

推荐答案

您的 i 遍历 sites 的元素而不是索引.试试 for(i in 1:length(sites)).或 x = read.xlsx(i,14).

Your i iterates through the elements of sites and not the index. Try for(i in 1:length(sites)) instead. Or x=read.xlsx(i,14).

这篇关于循环目录以获取Excel内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆