[R] Reading from files to 2D matrix

kparamas kparamas at asu.edu
Sun Jan 30 20:54:26 CET 2011


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!!
-- 
View this message in context: http://r.789695.n4.nabble.com/Reading-from-files-to-2D-matrix-tp3247581p3247581.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list