[英]Read many Excel files from R
It's my first time with R, so maybe the problem is trivial 这是我第一次使用R,所以也许这个问题很简单
I need to download date from xls files from url and each one should be in one data frame. 我需要从url从xls文件下载日期,每个文件应该在一个数据框中。 Like here .
喜欢这里
I decided to use gdata package (package 'xlsReadWrite' is not available for R version 3.1.0, RODBC not available for win64) 我决定使用gdata包(包'xlsReadWrite'不适用于R版本3.1.0,RODBC不适用于win64)
Downloading works great for one file (year eg = 2013 ) 下载适用于一个文件(年份,例如= 2013)
readxls<-function()
{
library("gdata")
link<-paste0("http://nbp.pl/kursy/archiwum/archiwum_tab_a_",year,".xls")
xlsdata <<- read.xls(link, sheet = 1, perl = "C:\\Perl64\\bin\\perl.exe", header=TRUE)
}
I tried to read many .xls into list() using loop. 我尝试使用循环将许多.xls读入list()。 (eg y_begin=2012, y_end=2014)
(例如y_begin = 2012,y_end = 2014)
readxls<-function()
{
library("gdata")
ldata<<- list()
j=1
for (i in y_begin:y_end)
{
link<-paste0("http://nbp.pl/kursy/archiwum/archiwum_tab_a_",i,".xls")
xlsdata <<- read.xls(link, sheet = 1, perl = "C:\\Perl64\\bin\\perl.exe", header=TRUE)
ldata[j] <<- xlsdata
j<-j+1
}
}
I thought that after that I'd be able to merge them, but don't know how to get data from single data frame in list for example > View(ldata[2]) returns only first collumn 我认为之后我能够合并它们,但不知道如何从列表中的单个数据帧获取数据,例如> View(ldata [2])仅返回第一列
Avoid use for
loop , specially for its side effect. 避免
for
循环,特别是其副作用。 Better to use lapply
here. 最好在这里使用
lapply
。 It is the R way to do things ( functional programming). 这是做事的R方式(函数式编程)。 Here I would do this:
我会这样做:
library(gdata)
readxls<-function()
{
ids <- seq(y_begin,y_end)
links <-paste0("http://nbp.pl/kursy/archiwum/archiwum_tab_a_",ids,".xls")
lapply (links,function(i)
read.xls(link, sheet = 1, perl = "C:\\Perl64\\bin\\perl.exe", header=TRUE)
)
}
This will return a list of data.frame, to merge them, assuming that they are ll the same: 这将返回data.frame列表,以合并它们,假设它们是相同的:
ll <- readxls()
do.call(rbind,ll)
Thank you so much, but the answer was easier. 非常感谢你,但答案更容易。 It's enought to make ldata[[j]]
它应该制作ldata [[j]]
readxls<-function()
{
library("gdata")
ldata<<- list()
j=1
for (i in y_begin:y_end)
{
link<-paste0("http://nbp.pl/kursy/archiwum/archiwum_tab_a_",i,".xls")
xlsdata <<- read.xls(link, sheet = 1, perl = "C:\\Perl64\\bin\\perl.exe", header=TRUE)
ldata[[j]] <<- xlsdata
j<-j+1
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.