[R] code to iterate function apply to matrix

Uwe Ligges ligges at statistik.tu-dortmund.de
Wed May 16 17:17:24 CEST 2012



On 16.05.2012 08:11, umai88 wrote:
> I got this code below and i want to repeat the loop for 100 times..

And what is the problem? What are you aiming at?

Uwe Ligges



>
> x<-rnorm(60)
> mat1<-matrix(x,nrow=15,ncol=4)
>
> trim<-numeric(ncol(mat1))
> win<-numeric(ncol(mat1))
> ssd<-numeric(ncol(mat1))
>
> for(j in 1:ncol(mat1))
> {
> n=length(mat1[,j])
> alpha=0.1
> k=floor(alpha*n)+1
> r=k-(alpha*n)
> i=k+1
> m=n-k
> y1<-sort(mat1[,j])
> y<-y1[i:m]
> x.low=(1-r)*y1[k+1]+r*y1[k]
> x.upp=(1-r)*y1[n-k]+r*y1[n-k+1]
> trim[j] =1/((1-2*alpha)*n)*(sum(y)+r*(y1[k]+y1[n-k+1]))
> win[j]=1/n*(sum(y)+k*(x.low+x.upp))
> ssd[j]<-sum((y-win[j])**2)+k*( (y1[k+1]-win[j])**2 + (y1[n-k]-win[j])**2 )
> }
>
> trim.mean<-matrix(trim, nrow=1)
> win.mean<-matrix(win, nrow=1)
> sum.sq.dev<-matrix(ssd, nrow=1)
>
> --
> View this message in context: http://r.789695.n4.nabble.com/code-to-iterate-function-apply-to-matrix-tp4630221.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.



More information about the R-help mailing list