-1

this code below is to read and loop thru multiple binary (365) files for one year in one folder. In fact I have many folders for many years, 365 files of year of 2000 in one folder, 365 files of year of 2001 in another folder and so on.

My question is how can I loop from one year to another (from one folder to another)?

setwd("C:\\PHD\\Climate Data\\Wind\\")
listfile<-dir()
for (i in c(1:365)) {
  conne <- file(listfile[i], "rb")
  file<- readBin(conne, integer(), size=2,  n=360*720, signed=T)
  file<-file-273.15 #
  close(conne)
  to.write = file(paste("C:\\PHD\\Climate Data\\Out\\Temperature_",i,".dat",sep=""),"wb")
  writeBin(file, to.write)
  close(to.write) 
}
joran
  • 169,992
  • 32
  • 429
  • 468
hkfidd
  • 3
  • 3
  • 1
    To improve your odds of getting help, you should probably "accept" past answers that have helped you (like [this one](http://stackoverflow.com/a/10090849/903061)) by clicking the checkmark to the left of the answer. – Gregor Thomas Apr 12 '12 at 17:09
  • Duplicate of your earlier question http://stackoverflow.com/questions/10032052/how-do-i-read-multiple-binary-files-in-r – Paul Hiemstra Apr 12 '12 at 17:21
  • Indeed, I've answered this question before... http://stackoverflow.com/questions/10032052/how-do-i-read-multiple-binary-files-in-r – Jeff Allen Apr 13 '12 at 14:47
  • @Amen, It sounds like the answers to your questions still aren't clear. I'd recommend reading a guide to getting started with R (something like http://www.luchsinger-mathematics.ch/Bashir.pdf) and then returning once you can clearly articulate what, specifically, you're having trouble with in this problem. – Jeff Allen Apr 13 '12 at 14:49

1 Answers1

0

There a number of steps you need to take:

  • You can use list.files to create a list of the files in the directory strucutre. Be sure to set recursive to TRUE in order to traverse the whole tree, and to set full.names to TRUE to get the names of the files including the subdirectory.
  • Once you have read this list of files I would use ldply form the plyr package to get all the files.

Finally, an example in (untested pseudo) code that reads all the data looks something like:

list_of_files = list.files("datadir", recursive = TRUE, full.names = TRUE)
dat = l_ply(list_of_files, function(fname) {
    dum_data = readBin(fname)
    writeBin(dum_data)
  })

dat will now be a data.frame which contains the data plus the associated time. Just replace the function with anything you like to get it working for your example.

Paul Hiemstra
  • 59,984
  • 12
  • 142
  • 149
  • Thanks a lot. However,It is a bit complex.I didn't clearly understand it.So now how do I insert the above loop which I wrote in this one which you have written. I did not really understand what you meant by this:(Just replace the function with anything you like to get it working for your example). – hkfidd Apr 12 '12 at 17:32
  • The bit of code you know have inside the for loop you can put into a function and use it in plyr. For more information on how plyr works in general, please read the paper that the author wrote at http://www.jstatsoft.org/v40/i01 – Paul Hiemstra Apr 12 '12 at 17:49
  • I changed the example to more closely match your problem – Paul Hiemstra Apr 12 '12 at 17:51
  • it seems that it is very advanced for my level. I do not really know how to use the code which you have provided. Anyway,thanks once again – hkfidd Apr 12 '12 at 18:05
  • If you read the paper I think things might become more clear. Good luck! – Paul Hiemstra Apr 12 '12 at 18:08