假设 7000 是一个实数而非近似值,并且每个文件中的所有数据结构相同(列数和行数相同):
setwd("/.../Prism Weather Data All/")
nc<- ## put the number of columns of each file (assuming they're all the same)
nr<- ## put the number of rows of each file (assuming they're all the same)
filenames <- list.files(path = "/.../Prism Weather Data All/", pattern = ".bil")
# initialize what is likely to be a large object
final.df<-as.data.frame(matrix(NA,ncol=7000*nc,nrow=nr))
counter=1
# loop through the files
for (i in filenames){
r = raster(i)
test <- as.data.frame(r, na.rm=TRUE)
final.df[,counter:counter+nc]<-test
counter<-counter+nc+1
}
# write the csv
write.csv(final.df,"final-filename.csv")
请记住,您的机器必须有足够的内存来保存所有数据,因为 R 需要在内存中有对象。
如果每个文件的列数不同,您可以通过调整循环内final.df 赋值中的索引并相应地增加counter 来进行调整。
编辑:产生预期结果
我认为 for 循环是完成此类工作的唯一方法。确实,7000 个文件是一个相当大的集合,所以希望花一些时间来查看它的迭代。
setwd("/.../Prism Weather Data All/")
nc<- ## put the number of columns you expect the data in the files to have
nr<- ## put roughly the number of rows times 12 (if you plan to read a year worth of data)
## PLUS some tolerance, so you'll end up with an object actually larger than needed
filenames <- list.files(path = "/.../Prism Weather Data All/", pattern = ".bil")
# initialize what is likely to be a large object
final.df<-as.data.frame(matrix(NA,ncol=c,nrow=nr))
counter=1
# loop through the files
for (i in filenames){
r = raster(i)
test <- as.data.frame(r, na.rm=TRUE)
numrow2<-nrow(test)
final.df[counter:counter+numrow2,]<-test
counter<-counter+numrow2+1
}
final.df[counter-1:nrow(final.df),]<-NULL ## remove empty rows
# write the csv
write.csv(final.df,"final-filename.csv")
希望对你有帮助。