|
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@x), size=2,to.write) } } |
|
Hello,
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 |
|
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) |
|
In reply to this post by Jonsson
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 <[hidden email]> 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@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. > > ______________________________________________ > [hidden email] 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. ______________________________________________ [hidden email] 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. |
|
In reply to this post by Jonsson
Hello,
round defaults to 0 decimal places. The second version uses 1, just as an example. From the help page: "round rounds the values in its first argument to the specified number of decimal places (default 0). " See ?round Rui Barradas |
|
The piece of R code given above is meant to perform calculations for many files(12) in one folder.But I am getting this error:Error: subscript out of bounds.It finished reading the first file(successfully) but the error arose when it started to read the next file. Any suggestions why?. I think the problem is in this first loop: for (n in 1:length(listfile)) { h=listfile[n]
b=file.info(h[n])$size/67420/4 |
|
Just a total guess but if h is the n-th element of listfile, do you
really mean to be taking the n-th element of h as well? It could be that when you get to the second element h[n] becomes h[2] which doesn't exist (h[1] obviously did) If you do mean this, listfile is probably a list (possibly of lists) so you want listfile[[n]] to get the n-th element (rather than a sublist) Hope this helps, Michael On Fri, Apr 27, 2012 at 1:28 PM, Jonsson <[hidden email]> wrote: > The piece of R code given above is meant to perform calculations for many > files(12) in one folder.But I am getting this error:Error: subscript out of > bounds.It finished reading the first file(successfully) but the error arose > when it started to read the next file. Any suggestions why?. I think the > problem is in this first loop: for (n in 1:length(listfile)) { h=listfile[n] > b=file.info(h[n])$size/67420/4 > > -- > View this message in context: http://r.789695.n4.nabble.com/Min-Max-tp4593065p4593279.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > [hidden email] 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. ______________________________________________ [hidden email] 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. |
|
Yes this what I meant. I think no need for this line h=listfile[[n]],if I can just put it directly in the other line to do the calculations for every file in my list.they are not really elements.they are files.
what I need is just this:get the size of each file in my list then /67420/4,to get the number of rows and assign that to b . so now b will be used later as a loop. I tried this also: for (n in 1:length(listfile)).{ h=listfile[[n]] b=file.info(h[[n]])$size/67420/4 } then I got this :Error in h[[n]] : subscript out of bounds |
|
In reply to this post by Michael Weylandt
this is what happened with me:that when I get to the second element h[n] becomes h[2] which
doesn't exist (h[1] obviously did). Any suggestions |
|
In reply to this post by Michael Weylandt
this is what happened with me:that when I get to the second element h[n] becomes h[2] which
doesn't exist (h[1] obviously did). Any suggestions |
|
a) don't double post
b) provide context... the majority of R-helpers do not use nabble c) read and believe the error message... don't subscript on a vector known to have one element. --------------------------------------------------------------------------- Jeff Newmiller The ..... ..... Go Live... DCN:<[hidden email]> Basics: ##.#. ##.#. Live Go... Live: OO#.. Dead: OO#.. Playing Research Engineer (Solar/Batteries O.O#. #.O#. with /Software/Embedded Controllers) .OO#. .OO#. rocks...1k --------------------------------------------------------------------------- Sent from my phone. Please excuse my brevity. Jonsson <[hidden email]> wrote: >this is what happened with me:that when I get to the second element >h[n] >becomes h[2] which >doesn't exist (h[1] obviously did). >Any suggestions > >-- >View this message in context: >http://r.789695.n4.nabble.com/Min-Max-tp4593065p4593402.html >Sent from the R help mailing list archive at Nabble.com. > >______________________________________________ >[hidden email] 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. ______________________________________________ [hidden email] 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. |
|
In reply to this post by Jonsson
Yes, like I said, you don't need to be subsetting h:
h = listoffiles[n] then later just use h Michael On Fri, Apr 27, 2012 at 2:22 PM, Jonsson <[hidden email]> wrote: > this is what happened with me:that when I get to the second element h[n] > becomes h[2] which > doesn't exist (h[1] obviously did). > Any suggestions > > -- > View this message in context: http://r.789695.n4.nabble.com/Min-Max-tp4593065p4593402.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > [hidden email] 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. ______________________________________________ [hidden email] 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. |
|
In reply to this post by Michael Weylandt
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@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])) |
|
Hello,
> > 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 > You're subtracting 273.15 to the row numbers, not to the values in those rows. Corrected: d.rows <- (d-1)*8 + 1:8 tm[ta[i],tb[i]]= round(10 * mean(D[d.rows, i] - 273.15)) Note that the d.rows extra variable is not really needed, it's there just for clarity. Rui Barradas |
|
In reply to this post by Jonsson
On Apr 28, 2012, at 9:54 AM, Jonsson wrote: > This code bellow will calculate average daily wind > speed(measurements are > taken every three hours). > snipped overly complex code not needed to address the semantic error below. > 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])) # > wrong You should not be subtracting the temperature offset within the index if your goal was to subtract the same number from each value in a matrix. Move it outside the index/extract function. You didn't get an error from "[" because it automatically truncated the floating point numbers you were passingwhich should have been integer vectors. tm[ta[i],tb[i]]= round(10 * mean(D[((d-1)*8 + 1:8)-273.15), i]-273.15)) (No effort made to check for the sensibility of the assignment to the 'tm' matrix.) Note: c(1,2)[2.5] #[1] 2 No error .... or even a warning. > > -- > 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. > > ______________________________________________ > [hidden email] 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. David Winsemius, MD West Hartford, CT ______________________________________________ [hidden email] 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. |
|
Dear David, I think you meant tm[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. |
|
In reply to this post by Michael Weylandt
Hello again,
I am still having the same problem with the main loop.it succeeded to finish reading and writing from the first file in my listfile but failed when it moves to the second file giving this error:Error: subscript out of bounds 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=listfile[n] b=file.info(h)$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@x), size=2,to.write) } } |
|
In reply to this post by Jonsson
On Apr 28, 2012, at 11:51 AM, Jonsson wrote: > Dear David, I think you meant tm[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 ______________________________________________ [hidden email] 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. |
|
In reply to this post by Jonsson
Hello,
> > Hello again, > I am still having the same problem with the main loop.it succeeded to finish reading and writing from the first file in my > listfile but failed when it moves to the second file giving this error:Error: subscript out of bounds > Don't subscript then. The only place where you're using the subscript is in h=listfile[n] The same can be made using the vector 'listfile' directly, for instance, (dummy loop, just to see if it works.) for(h in listfile){ print(h) print(file.info(h)$size) } Hope this helps, Rui Barradas |
|
Yes ,It worked. So how can I insert it in my code? Please
for(h in listfile){ + print(h) + print(file.info(h)$size) + } [1] "Wind_WFD_200101.nc.img" [1] 66880640 [1] "Wind_WFD_200102.nc.img" [1] 60408320 [1] "Wind_WFD_200103.nc.img" [1] 66880640 [1] "Wind_WFD_200104.nc.img" [1] 64723200 [1] "Wind_WFD_200105.nc.img" [1] 66880640 [1] "Wind_WFD_200106.nc.img" [1] 64723200 [1] "Wind_WFD_200107.nc.img" [1] 66880640 [1] "Wind_WFD_200108.nc.img" [1] 66880640 [1] "Wind_WFD_200109.nc.img" [1] 64723200 [1] "Wind_WFD_200110.nc.img" [1] 66880640 [1] "Wind_WFD_200111.nc.img" [1] 64723200 [1] "Wind_WFD_200112.nc.img" [1] 66880640 |
| Powered by Nabble | Edit this page |
