1

The piece of R code given below is meant to perform calculations for many files (12) in one folder. But I am getting this error:

Error: subscript out of bounds.

It finished reading the first file but the error arose when it started to read the next file. Any suggestion why?

library(Matrix)
setwd("C:\\Users\\Desktop\\img")
listfile <- dir()
long <- file("C:\\Users\\Desktop\\New folder (5)\\vfd.bin", "rb")
A = readBin(long, integer(), size=2,n=67420*1, signed=F)
ta <- t(A)
lot <- file("C:\\Users\\Desktop\\New folder (5)\\lat.img", "rb")
B = readBin(lot, integer(), size=2,n=67420*1, signed=F)
tb <-t (B)

for (n in 1:length(listfile))
{
    h = listfile[n]
    b = file.info(h[n])$size/67420/4
    wind <- file(h, "rb")
    C = readBin(wind, double(), size=4,n=67420*b, signed=TRUE)
    D <- matrix(C,nrow=b,ncol=67420)
    for(d in 1:b)
    {
        M <- Matrix(-9999, 360, 720)
        tm <- t(M)
        for(i in 1:67420)
        {
            tm[ta[i],tb[i]] = round(10 * ((D[(d-1)*8+1,i] + D[(d-1)*8+2,i] + D[(d-1)*8+3,i] + D[(d-1)*8+4,i] + D[(d-1)*8+5,i] + D[(d-1)*8+6,i] + D[(d-1)*8+7,i] + D[(d-1)*8+8,i] ) / 8))
        } ###gooooooood
        to.write <- sprintf("C:\\Users\\Desktop\\New folder (6)\\Yar_%00d.bin", d)
        writeBin(as.integer(tm@x), size=2,to.write)
    }
}
Artem
  • 3,304
  • 3
  • 18
  • 41
Sami Yemein
  • 125
  • 2
  • 8
  • 3
    You should put some time into capitalizing the beginnings of sentences, putting spaces after periods and spelling. I got tired after the 10th grammatical error. The human brain can only handle so much lexical noise. – IRTFM Apr 25 '12 at 22:17
  • Please pare down the data to the point where it can be created in your example. Other than that, it appears that you want to write to M, using row and columns taken from A and B, with values from C. It appears that C is a lot longer than A or B, so only the first 67420 elements of C are used in your loop. – Matthew Lundberg Apr 26 '12 at 01:13
  • In addition, it pains me to see the definition of "wind" and "C" each time through the loop. Move these definitions to the outside of the "for" loop. – Matthew Lundberg Apr 26 '12 at 01:15
  • sorry it is my bad English.I did my best to write it better.Matthew, you r right, c is a lot longer than A and B and this loop will write to M ,using rows and columns taken from A and B,with values from C.So how can I improve it to take then the next 67420 and write it to new file and so on till the 248th 67420. Many thanks for both of you – Sami Yemein Apr 26 '12 at 07:16
  • 1
    Million thanks once again for corrections – Sami Yemein Apr 26 '12 at 07:32
  • Wrap your code in a function, get a list of the files with `list.files` and loop over the files using a `plyr` function like ldply, or laply. See also http://stackoverflow.com/questions/9982269/applying-r-script-prepared-for-single-file-to-multiple-files-in-the-directory/9983680#9983680 for some tips. – Paul Hiemstra Apr 27 '12 at 13:59

1 Answers1

0

The meaning of the error is that you're trying to access an vector element out of its boundary.

The problem is located in this piece of code (first two lines after for loop initialization):

h = listfile[n]
b = file.info(h[n])$size/67420/4

To eliminate error you should substitute b = file.info(h[n]) to b = file.info(h).

It's because of when n = 1 (first iteration of the loop) h equals to the first image file name (it is vector value of length 1, de-facto scalar) and h[1] equals to h. Everything seems OK on the first iteration.

But on the second iteration (n = 2) h equals to the second image file name (it is scalar of length 1), but you are trying to access the second element of 1-length vector. So b value on the second iteration will be NA and further in code the error will be thrown.

Artem
  • 3,304
  • 3
  • 18
  • 41