# [R] Min , Max

Fri Apr 27 18:14:40 CEST 2012

```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")
>  ta<-t(A)
>  lot <- file("C:\\Users\\aalyaari\\Desktop\\New folder (5)\\lat.img",
> "rb")
>  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")
>
>  	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,