-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

Sam DeHaan
  • 10,246
  • 2
  • 40
  • 48
hkfidd
  • 3
  • 3
  • 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.htm – Mark Miller Apr 05 '12 at 16:18
  • Perhaps a helpful starting point: http://stackoverflow.com/questions/6420207/manipulating-multiple-files-in-r – Ben Bolker Apr 05 '12 at 18:07
  • See also your other question http://stackoverflow.com/questions/10128084/how-do-i-read-multiple-binary-files-in-in-different-folders-in-r – Paul Hiemstra Apr 12 '12 at 17:54

1 Answers1

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.

Jeff Allen
  • 17,277
  • 8
  • 49
  • 70
  • 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[[(i*4):(i*4+3)]]) writeBin(results)} – hkfidd Apr 06 '12 at 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[[(i*4):(i*4+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 help – hkfidd Apr 06 '12 at 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 Apr 06 '12 at 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 Apr 06 '12 at 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=365*360*720, signed=F) results <- mean(listfile[[(i*4):(i*4+3)]]) results1<- writeBin(results) close(conne) } – hkfidd Apr 06 '12 at 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 Apr 07 '12 at 15:25
  • https://www.dropbox.com/home here is one file out of 365 files in one folder that I want to work on – hkfidd Apr 09 '12 at 14:41