song song
2010-May-03 17:11 UTC
[R] how to rewrite this for loops in matrix form without loop
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]]
Dennis Murphy
2010-May-03 17:28 UTC
[R] how to rewrite this for loops in matrix form without loop
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> as.vector(do.call(c, lapply(split(x0, y0), mean)))[1] -0.18922774 0.36333115 -0.04295032 -0.13892563 -0.03968301 0.33326034 [7] 0.28649576 -0.03786830 HTH, Dennis On Mon, May 3, 2010 at 10:11 AM, song song <rprojecthelp@gmail.com> 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] > } > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help@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. >[[alternative HTML version deleted]]
Henrique Dallazuanna
2010-May-03 17:35 UTC
[R] how to rewrite this for loops in matrix form without loop
Try this: rowsum(x0, y0)[,1]/table(y0) On Mon, May 3, 2010 at 2:11 PM, song song <rprojecthelp@gmail.com> 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] > } > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help@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. >-- Henrique Dallazuanna Curitiba-Paraná-Brasil 25° 25' 40" S 49° 16' 22" O [[alternative HTML version deleted]]
Joshua Wiley
2010-May-03 17:37 UTC
[R] how to rewrite this for loops in matrix form without loop
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 <rprojecthelp at gmail.com> 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] > } > > ? ? ? ?[[alternative HTML version deleted]] > > ______________________________________________ > 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. >-- Joshua Wiley Senior in Psychology University of California, Riverside http://www.joshuawiley.com/