R does not recognise columns and rows as they are supposed to be
X<-c(364:369) ; Y<-c(82:92) ##### for sellected region
...
file2<-matrix(data=file,ncol=720,nrow=360)
extract[i]<-mean(file2[X,Y],na.rm=TRUE)
Note that 2-dimensional subscripts are given as
mat[ROWS, COLUMNS]
and you are using the reverse order, hence you get the
error message that you are asking for rows 364:369 of
a 360-row matrix.
-----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Rui Barradas Sent: Thursday, May 24, 2012 9:48 AM To: r-help at r-project.org Subject: Re: [R] R does not recognise columns and rows as they are supposed to be Hello, There are several things with your code, most of which are not errors. 1. X<-c(364:369) ; Y<-c(82:92 and later c(1:365) Expressions of the form m:n are integer sequences, the c() is not needed. 2. extract<-vector() First you create the vector, then keep extending it throughout the loop. This is much slower. If you know the final length (and type), create it like this: extract <- numeric(365) # or double(365) 3. 'dir1' is not used. 4. Now the readBin instruction: 4.1. 'file' is the name of a function, use, say, 'file1' instead. 4.2. Option 'signed' defaults to TRUE but it's only valid for integer types. 5. And, maybe this is the problem, in R, doubles are 8 bytes, not 4. Get rid of option 'size', like the help page says, "If size is specified and not the natural size of the object, each element of the vector is coerced to an appropriate type before being written or as it is read." You are NOT reading doubles.
The file may well contain single precision numbers and using what="double", size=4 will read them and convert them to doubles so other functions in R can use them. (Likewise, readBin can read files containing 1- or 2-byte signed or unsigned integers and store them as the 4-byte signed integers that the rest of R can deal with.) If the file was created on another sort of machine, then you may have worry about the byte order - try adding endian="big" or endian="little".
As for the rest, it seems ok to me. (Keep option 'n' in readBin.) Hope this helps, Rui Barradas Jonsson wrote
Dear All,
The code given bellow is to extract values of one region and write that
to a text file(there are 365 binary files in the directory).
The problem which I am facing is that all my files are binary with size of
360 rows and 720 columns.
I specified that in this line:
file2<-matrix(data=file,ncol=720,nrow=360) but I got an error : Error in
mean(file2[X, Y], na.rm = TRUE) : subscript out of bounds.
and then I rewrote the above line as
:file2<-matrix(data=file,ncol=360,nrow=720.I put ncol=360 and nrows =720
which is not right.But that worked and I didn't get any error.however,the
results were not correct.
Any help please
X<-c(364:369) ; Y<-c(82:92) ##### for sellected region
extract<-vector()
dir1<- list.files("C:\\Users\\aalyaari\\Desktop\\New folder
(10)\\Climate_Rad_f_GAMMA_%d.img", full.names = TRUE)
listfile<-dir()
for (i in c(1:365)) {
conne <- file(listfile[i], "rb")
file<- readBin(conne, double(), size=4, n=720*360, signed=T)
file2<-matrix(data=file,ncol=720,nrow=360)
extract[i]<-mean(file2[X,Y],na.rm=TRUE)
close(conne)
write.table(extract,"C:\\Users\\aalyaari\\Desktop\\New folder
(10)\\sam.txt")}
-- View this message in context: http://r.789695.n4.nabble.com/R-does-not-recognise- columns-and-rows-as-they-are-supposed-to-be-tp4631217p4631227.html Sent from the R help mailing list archive at Nabble.com.
______________________________________________ R-help at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.