Hellow everyone, This code bellow will calculate average daily wind speed(measurements are taken every three hours).Any ideas how to take the Min and Max instead of average. library(Matrix) setwd("C:\\Users\\aalyaari\\Desktop\\img") listfile<-dir() 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) for (n in 1:length(listfile)) { #h[n]=listfile[n] h=listfile[n] #b[n]=file.info(h[n])$size/67420/4 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)) } to.write <- sprintf("C:\\Users\\aalyaari\\Desktop\\New folder (6)\\Yar_%00d.bin", d) writeBin(as.integer(tm at x), size=2,to.write) } } -- View this message in context: http://r.789695.n4.nabble.com/Min-Max-tp4593065p4593065.html Sent from the R help mailing list archive at Nabble.com.
Hello, Jonsson wrote> > Hellow everyone, > This code bellow will calculate average daily wind speed(measurements are > taken every three hours).Any ideas how to take the Min and Max instead of > average. > > library(Matrix) > setwd("C:\\Users\\aalyaari\\Desktop\\img") > listfile<-dir() > 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) > > for (n in 1:length(listfile)) > { > > #h[n]=listfile[n] > h=listfile[n] > #b[n]=file.info(h[n])$size/67420/4 > 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)) > > } > to.write <- sprintf("C:\\Users\\aalyaari\\Desktop\\New folder > (6)\\Yar_%00d.bin", d) > writeBin(as.integer(tm at x), size=2,to.write) > } > } >Your code line with 'round', near the bottom, is equivalent to this simpler line: tm[ta[i],tb[i]]= round(10 * sum(D[(d-1)*8 + 1:8, i]) / 8) Note that this doesn't give the average, but the average multiplied by 10. For the average, with and without the factor 10, use tm[ta[i],tb[i]]= round(10 * mean(D[(d-1)*8 + 1:8, i])) tm[ta[i],tb[i]]= round(mean(D[(d-1)*8 + 1:8, i])) (Why not use the parameter 'digits'?) tm[ta[i],tb[i]]= round(mean(D[(d-1)*8 + 1:8, i]), 1) Now just substitute min or max for mean. Hope this helps, Rui Barradas -- View this message in context: http://r.789695.n4.nabble.com/Min-Max-tp4593065p4593118.html Sent from the R help mailing list archive at Nabble.com.
Dear Rui, I really appreciate your help. This is exactly what I was looking for.I know there is simpler way than mine but I couldn't figure it out.So, many many thanks. I wonder what is the difference between This: tm[ta[i],tb[i]]= round(mean(D[(d-1)*8 + 1:8, i])) and This: (Why not use the parameter 'digits'?) tm[ta[i],tb[i]]= round(mean(D[(d-1)*8 + 1:8, i]), 1) -- View this message in context: http://r.789695.n4.nabble.com/Min-Max-tp4593065p4593159.html Sent from the R help mailing list archive at Nabble.com.
You might want to look into the caTools package: it provides runmean, runmin, and runmax functions -- if you set those for a window of length 8 and then take every 8th element, you should get what you're looking for (much cleaner and faster too!) A good way to do that subset is something like: x <- 1:100 y <- x^2 - 3*x + 5 n <- 8 y[!(seq_along(x) %% n)] Though I'm pretty sure I've seen a cleaner version elsewhere.... Michael On Fri, Apr 27, 2012 at 11:46 AM, Jonsson <amen.alyaari at bordeaux.inra.fr> wrote:> Hellow everyone, > This code bellow will calculate average daily wind speed(measurements are > taken every three hours).Any ideas how to take the Min and Max instead of > average. > > library(Matrix) > setwd("C:\\Users\\aalyaari\\Desktop\\img") > listfile<-dir() > 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) > > for (n in 1:length(listfile)) > { > > ? ? ? ?#h[n]=listfile[n] > ? ? ? ?h=listfile[n] > ? ? ? ?#b[n]=file.info(h[n])$size/67420/4 > ? ? ? ?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)) > > ? ? ? ? ? ? ? ?} > ? ? ? ? ? ? ? ?to.write <- sprintf("C:\\Users\\aalyaari\\Desktop\\New folder > (6)\\Yar_%00d.bin", d) > ? ? ? ? ? ? ? ?writeBin(as.integer(tm at x), size=2,to.write) > ? ? ? ?} > } > > > -- > View this message in context: http://r.789695.n4.nabble.com/Min-Max-tp4593065p4593065.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.
This code bellow will calculate average daily wind speed(measurements are taken every three hours). library(Matrix) setwd("C:\\Users\\aalyaari\\Desktop\\img") listfile<-dir() 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) for (n in 1:length(listfile)) { #h[n]=listfile[n] h=listfile[n] #b[n]=file.info(h[n])$size/67420/4 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 * mean(D[(d-1)*8 + 1:8), i]) } to.write <- sprintf("C:\\Users\\aalyaari\\Desktop\\New folder (6)\\Yar_%00d.bin", d) writeBin(as.integer(tm at x), size=2,to.write) } } the values are measured in kelvin so i want to convert them to degree,therefor I want to subtract all values by 273.15 before talking the average or max.... but did not work.In other words, i want this: every value is subtracted by 273.15 first ,then do other calculations(Min, Max)Can anyone tell me what is wrong.Thanks tm[ta[i],tb[i]]= round(10 * mean(D[((d-1)*8 + 1:8)-273.15), i])) -- View this message in context: http://r.789695.n4.nabble.com/Min-Max-tp4593065p4594774.html Sent from the R help mailing list archive at Nabble.com.
On Apr 28, 2012, at 11:51 AM, Jonsson wrote:> Dear David, I think you meanttm[ta[i],tb[i]]= round(10 * mean(D[((d-1)*8 + 1:8),i]-273.15)) not tm[ta[i],tb[i]]= round(10 * mean(D[((d-1)*8 + 1:8)-273.15), i]-273.15)) .> > As you can see in the code above.I want to save the results as > integer(16bit) thats why I multiplied by 10(as factor),in order to > keep one > dice-mil . Because I will later divide by 10.for example if I got 25 > so this > means 2.5. Thats why I used round.Yes that is what I meant. -- David Winsemius, MD Heritage Laboratories West Hartford, CT
Hello,> > Yes ,It worked. So how can I insert it in my code? Please >Simply like this: #for (n in 1:length(listfile)) #{ # h=listfile[n] # for(h in listfile) { Rui Barradas -- View this message in context: http://r.789695.n4.nabble.com/Min-Max-tp4593065p4595458.html Sent from the R help mailing list archive at Nabble.com.
Reasonably Related Threads
- R does not recognise columns and rows as they are supposed to be
- write to M, using row and columns taken from A and B, with values from C
- How to calculate the spatial correlation of several files?
- Why R order files as 1 10 100 not 1 2 3 ?
- read multiaple files within one folder