Amen
2012-Apr-26 08:25 UTC
[R] write to M, using row and columns taken from A and B, with values from C
I want to write to M, using row and columns taken from A and B, with values from C. C is a lot longer than A and B, so only the first 67420 elements of C are used in my loop.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 library(Matrix) M <- Matrix(-9999, 360, 720) ## creat matrix with 720 columns and 360 ro ws with valus of -9999 long <- file("C:\\New folder (5)\\inra.bin", "rb") A = readBin(long, integer(), size = 2, n = 1*67420, signed = TRUE) lot <- file("C:\\New folder (5)\\lat.img", "rb") B = readBin(lot, integer(), size = 2, n = 1*67420, signed = TRUE) wind <- file("C:\\Wind_WFD_200201.bin", "rb") C= readBin(wind, integer(), size = 2, n = 248*67420, signed = TRUE) ##it has 67420 columns and 248 rows for (i in seq_along(C)){ for (i in (1:67420)) { M(A(i), B(i)) = C(i) }} for (i in seq_along(M)){ fileName <- sprintf("C:/New folder/glo_%d.flt", i) writeBin(as.double(M[[i]]), fileName, size = 4)} ## for writing each row to to a new file -- View this message in context: http://r.789695.n4.nabble.com/write-to-M-using-row-and-columns-taken-from-A-and-B-with-values-from-C-tp4589197p4589197.html Sent from the R help mailing list archive at Nabble.com.
David Winsemius
2012-Apr-26 12:45 UTC
[R] write to M, using row and columns taken from A and B, with values from C
On Apr 26, 2012, at 4:25 AM, Amen wrote:> I want to write to M, using row and columns taken from A and B, with > values > from C. C is a lot longer than A and B, so only the first 67420 > elements of > C are used in my loop.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 > > > library(Matrix) > M <- Matrix(-9999, 360, 720) ## creat matrix with 720 columns and > 360 ro > ws with valus of -9999 > long <- file("C:\\New folder (5)\\inra.bin", "rb") > A = readBin(long, integer(), size = 2, n = 1*67420, signed = TRUE) > lot <- file("C:\\New folder (5)\\lat.img", "rb") > B = readBin(lot, integer(), size = 2, n = 1*67420, signed = TRUE) > wind <- file("C:\\Wind_WFD_200201.bin", "rb") > C= readBin(wind, integer(), size = 2, n = 248*67420, signed = > TRUE) ##it > has 67420 columns and 248 rows > for (i in seq_along(C)){ > for (i in (1:67420)) { > M(A(i), B(i)) = C(i)You cannot use parens for such an assignment, since A, B and C are not R functions (or mathematical ones even.) You need the '[<-' function and its cbind form of arguments. Read ?'[<-' It is possible to construct an assignment (without a loop) of the form: # M needs to exist but you did pre-allocate it. M[ cbind(A, B) ] <- C However, C does need to be of the same length as A and B for this to succeed. At the moment C appears to be too large (by a factor of 248) and not of the right dimension for there to be a 1-1 correspondence between the C values and the (A,B) pairs.> }} > for (i in seq_along(M)){ > fileName <- sprintf("C:/New folder/glo_%d.flt", i)Now that does seem strange. Do you really mean to create aew file for every single value in M???> writeBin(as.double(M[[i]]),Wait! "[[" is for lists. M is a matrix.> fileName, size = 4)} ## for writing each row to > to a new fileI think you need to go back and review basic R syntax, and then review your code and check for the correspondence of object dimensions and the semantics of your assignment goals. As outlines in the Posign Guide. You will get further along and get better tyested advice if you construct a small test case before sending the question to r-help. The error messages at the console _are_ very informative, but if they are uncler you should copy them in full in your next posting. -- David Winsemius, MD West Hartford, CT
Amen
2012-Apr-27 06:22 UTC
[R] write to M, using row and columns taken from A and B, with values from C
Thanks a lot for your reply. I have managed to do it but still something that I do not know how to do that.This code below will read the first row in D and write it to a new file.I want to make a another loop to read all rows and write each result of every row to a file. library(Matrix) M <- Matrix(-9999, 360, 720)## creat matrix with 720 columns and 360 rows ith valus of -9999 tm<-t(M) long <- file("C:\\Users\\aalyaari\\Desktop\\New folder (5)\\inra.bin", "rb") A=readBin(long, integer(), size=2,n=67420*1, signed=F) ta<-t(A) lot <- file("C:\\Users\\aalyaari\\Desktop\\New folder (5)\\lat.img", "rb") B=readBin(lot, integer(), size=2,n=67420*1, signed=F) tb<-t(B) wind <- file("C:\\Users\\aalyaari\\Desktop\\Wind_WFD_200201.bin", "rb") C=readBin(wind, double(), size=4,n=67420*248, signed=TRUE) D<-matrix(C,nrow=248,ncol=67420) for(i in 1:67420){tm[ta[i],tb[i]]=D[1,i]} to.write file(paste("C:\\Users\\aalyaari\\Desktop\\Yar15.bin",sep=""),"wb") writeBin(as.real(tm), size=4,to.write) -- View this message in context: http://r.789695.n4.nabble.com/write-to-M-using-row-and-columns-taken-from-A-and-B-with-values-from-C-tp4589197p4591852.html Sent from the R help mailing list archive at Nabble.com.