Skip to content

Reading from files to 2D matrix

1 message · kparamas

#
Hi,

I am reading from many files DYNAMICALLY into a 2D matrix using row bind. 
I am not able to do with the code below.


for(years in c(1:NUM_YEARS))
{
    for(weeks in seq(1,NUM_WEEKS,LAG_WEEKS))
    {
        currentYear = BASE_YEAR + years;

            if(weeks < 9)
                fileName = sprintf("%d%s%d%s%d%s", currentYear, "/diffw0",
(weeks+1), "y", currentYear, "+landmask")
            else
                fileName = sprintf("%d%s%d%s%d%s", currentYear, "/diffw",
(weeks+1), "y", currentYear, "+landmask")

            #print(fileName)

            dataval = readBin(file(fileName, "rb"), double(),
size=4,n=ROW*COL, endian="little")
            dataval = dataval[abs(dataval[]) < 100]
            dataval = dataval[abs(dataval[]) != 0]

            cData = rbind(dataval)
            dim(cData)
    }
}

I tried readBin and when it has 30000 values in it,

when I read them sepeartely as dataval1, dataval2,..
and then do cData=rbind(dataval1, dataval2,....)
It works perfectly!!