How do I read multiple binary files in R?

Go To StackoverFlow.com

-3

Suppose we have files in one folder file1.bin, file2.bin, ... , and file1460.bin in directory C:\R\Data and we want to read them and make a loop to go from 1 to 4 and take the average then from 4 to 8 average and so on till 1460.in the end will get 360 files I tried to have them in a list,but did not know how to make the loop.

How do I read multiple files and manupulat them? in R language
I have been wasting countless hourse to figuer it out.any help

2012-04-05 16:05
by hkfidd
Here is a link that might be helpful, although the link does not seem to address *.bin files specifically. http://www.ats.ucla.edu/stat/r/code/read_multiple.ht - Mark Miller 2012-04-05 16:18
Perhaps a helpful starting point: http://stackoverflow.com/questions/6420207/manipulating-multiple-files-in- - Ben Bolker 2012-04-05 18:07
See also your other question http://stackoverflow.com/questions/10128084/how-do-i-read-multiple-binary-files-in-in-different-folders-in- - Paul Hiemstra 2012-04-12 17:54


0

results <- array(dim=360)
for (i in 1:360){
  results <- mean(yourlist[[(i*4):(i*4+3)]])
}

YMMV with the mean(yourList) call, but that structure would be how you could loop through the data once it's loaded.

2012-04-05 16:14
by Jeff Allen
Thanks alot.in fact here is my code listfile<-dir("C:\PHD\Climate Data\Wind\") results <- array(0, dim=c(365,720,360))

for (i in 1:365){ results <- mean(listfile[[(i4):(i4+3)]]) writeBin(results) - hkfidd 2012-04-06 12:09

Sorry ignore the previous .Thanks alot.in fact here is my code listfile<-dir("C:\PHD\Climate Data\Wind\") results <- array(0, dim=c(365,720,360)) for (i in 1:365){ results <- mean(listfile[[(i4):(i4+3)]]) writeBin(results)} but I got this Error: in listfile[[(i * 4):(i * 4 + 3)]] : attempt to select more than one element. what I want to do is to read those 365 binary files and then take the averag for every four files and then write to binary so I will get finally 40 files.Any hel - hkfidd 2012-04-06 12:21
As @Mark suggested, you may be better off looking at a resource like: http://www.ats.ucla.edu/stat/r/code/read_multiple.htm. It's not really clear what your specific problem is. If you can post a couple of the files online, maybe I could revise the answer.

The basic idea is that you'll load all of the files into a list, then loop through and take the mean of every 4 using something like the loop above - Jeff Allen 2012-04-06 14:27

Thanks Jeff.where can I upload the files so you can have alook on them or if you give me your email,i send them to - hkfidd 2012-04-06 14:37
is this right??? Testarray<-array(0, dim=c(1460,720,360)) listfile<-dir("C:\PHD\Climate Data\Wind\") for (i in c(1:1460)) { Testarray <- file(listfile[i], "rb") Testarray[i,,]<- readBin(conne, integer(), size=2, n=365360720, signed=F) results <- mean(listfile[[(i4):(i4+3)]]) results1<- writeBin(results) close(conne) - hkfidd 2012-04-06 14:39
Without access to your data, I really have no idea if that's right. Typically, people post it publicly online and share the URL in the question - Jeff Allen 2012-04-07 15:25
https://www.dropbox.com/home here is one file out of 365 files in one folder that I want to work o - hkfidd 2012-04-09 14:41
Ads