는 실수하지 근사치이며, 각 파일의 모든 데이터는 동일 구조 (열과 행의 같은 수의)됩니다 : 컴퓨터가이 점을 명심
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")
희망이 있습니다.
'values (r)'은 아마도'as.data.frame (r)'보다 빠를 것입니다. – jbaums
as.data.frame (stack (filenames))을 시도해보십시오. 그러나 이것을 처리하는 방법은 많습니다. – mdsumner