Barry
Barry

Reputation: 739

How to extract values that correspond to numbers in files's names?

I have one file (matrix) time that its values are hours from 0 to 23. This file can be read as:

  conne1 <- file("/dat/file_time.bin","rb")
  file_time<- readBin(conne1, numeric(), size=4,  n=1000*500, signed=TRUE)

Now I have other 24 files that have the same dim as time and can be read exactly as time:

dir1=list.files("/data/files", "*.bin", full.names = TRUE)
for(i in 1:length(dir1)){
readBin(dir1[i], numeric(), size = 4 ,n = 1000 * 500 , signed = T)
……………}

These 24 files are named like this:

File1_00.bin;File1_01.bin;.....Until......File1_23.bin

What I need is just to start reading values in time and ,for example, if the value is 12 in time, go to the file named File1_12.img in dir1and extract the corresponding value and so on. At the end I have one file liketime but it has corresponding values from the 24 files.

Upvotes: 0

Views: 99

Answers (1)

nicola
nicola

Reputation: 24480

Can't reproduce, but I'd try the following, assuming that I understood what you want:

 #read all the files and store in a matrix
 allContent<-do.call(cbind,lapply(dir1,
                           function(x) readBin(x, numeric(), size = 4 ,n = 1000 * 500 , signed = T)))
 #subset allContent to get the desired values
 allContent[cbind(1:(1000*500),file_time+1)]

To make a test:

 #create the file time sampling randomly between 0 and 23
 set.seed(1234)
 n<-1383 * 586
 file_time<-runif(n,0,23)
 #creating an n x 24 matrix with values between 0 and 9999
 allContent<-matrix(runif(n*24,0,9999),ncol=24)
 #subsetting
 allContent[cbind(1:n, file_time + 1)]

Just the last edit to explain last line. You can subset a matrix by providing a two-column matrix in which each row indicates the element to take. In my line, the cbind(1:n, file_time + 1) forms a matrix whose first row is made by 1 and the first value of file_time, the second by 2 and the second value of file_time and so on. Since each column of allContent is the content of the i-th file, I'm saying that the first value of the output must be the first value of the file_time[1]th column and so on.

If I wasn't clear enough, you can chech ?"[", section Matrices and arrays, the third form of indexing.

Upvotes: 2

Related Questions