Re: [R] how to rewrite this for loops in matrix form without loop

2010-05-03 Thread Henrique Dallazuanna
Or better: tapply(x0, y0, mean) On Mon, May 3, 2010 at 2:35 PM, Henrique Dallazuanna wrote: > Try this: > > rowsum(x0, y0)[,1]/table(y0) > > > On Mon, May 3, 2010 at 2:11 PM, song song wrote: > >> x0=rnorm(100) >> y0=rpois(100,3)+1 >> >> ind=as.data.frame(table(y0)) >> >> ind1=ind[,1] >> ind2=i

Re: [R] how to rewrite this for loops in matrix form without loop

2010-05-03 Thread Joshua Wiley
I believe you are trying to find the mean of x0 for each level (group) of y0. try this: by(x0, y0, mean) or if you want a vector (e.g., to merge into a matrix) c(by(x0, y0, mean)) Best regards, Josh On Mon, May 3, 2010 at 10:11 AM, song song wrote: > x0=rnorm(100) > y0=rpois(100,3)+1 > > in

Re: [R] how to rewrite this for loops in matrix form without loop

2010-05-03 Thread Henrique Dallazuanna
Try this: rowsum(x0, y0)[,1]/table(y0) On Mon, May 3, 2010 at 2:11 PM, song song wrote: > x0=rnorm(100) > y0=rpois(100,3)+1 > > ind=as.data.frame(table(y0)) > > ind1=ind[,1] > ind2=ind[,2] > > phi=NULL > for (i in 1:length(ind2)){ > phi[i]=sum(x0[y0==ind1[i]])/ind2[i] > } > >[[alterna

Re: [R] how to rewrite this for loops in matrix form without loop

2010-05-03 Thread Dennis Murphy
as.vector(do.call(c, lapply(split(x0, y0), mean))) Test with data generated according to your code: > phi=NULL > for (i in 1:length(ind2)){ + phi[i]=sum(x0[y0==ind1[i]])/ind2[i] + } > phi [1] -0.18922774 0.36333115 -0.04295032 -0.13892563 -0.03968301 0.33326034 [7] 0.28649576 -0.03786830 > a

[R] how to rewrite this for loops in matrix form without loop

2010-05-03 Thread song song
x0=rnorm(100) y0=rpois(100,3)+1 ind=as.data.frame(table(y0)) ind1=ind[,1] ind2=ind[,2] phi=NULL for (i in 1:length(ind2)){ phi[i]=sum(x0[y0==ind1[i]])/ind2[i] } [[alternative HTML version deleted]] __ R-help@r-project.org mailing list http