Hi, I have gote the following data x1 <- c(rep(1,6),rep(4,7),rep(6,10)) x2 <- rnorm(length(x1),6,1) data <- data.frame(x1,x2) and I would like to compute the mean of the x2 for each individual of x1, i. e. x1=1,4 and 6? Thank you very much in advance, Amori [[alternative HTML version deleted]]
have a look at ?tapply(), e.g., x1 <- c(rep(1,6),rep(4,7),rep(6,10)) x2 <- rnorm(length(x1),6,1) tapply(x1, x1, mean) I hope it helps. Best, Dimitris amor Gandhi wrote:> Hi, > > I have gote the following data > > x1 <- c(rep(1,6),rep(4,7),rep(6,10)) > x2 <- rnorm(length(x1),6,1) > data <- data.frame(x1,x2) > > and I would like to compute the mean of the x2 for each individual of x1, i. e. x1=1,4 and 6? > > Thank you very much in advance, > > Amori > > > > [[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. >-- Dimitris Rizopoulos Assistant Professor Department of Biostatistics Erasmus University Medical Center Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands Tel: +31/(0)10/7043478 Fax: +31/(0)10/7043014
sorry, that should be tapply(x2, x1, mean) Best, Dimitris Dimitris Rizopoulos wrote:> have a look at ?tapply(), e.g., > > x1 <- c(rep(1,6),rep(4,7),rep(6,10)) > x2 <- rnorm(length(x1),6,1) > > tapply(x1, x1, mean) > > > I hope it helps. > > Best, > Dimitris > > > amor Gandhi wrote: >> Hi, >> >> I have gote the following data >> >> x1 <- c(rep(1,6),rep(4,7),rep(6,10)) >> x2 <- rnorm(length(x1),6,1) >> data <- data.frame(x1,x2) >> >> and I would like to compute the mean of the x2 for each individual of >> x1, i. e. x1=1,4 and 6? >> >> Thank you very much in advance, >> >> Amori >> >> >> [[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. >> >-- Dimitris Rizopoulos Assistant Professor Department of Biostatistics Erasmus University Medical Center Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands Tel: +31/(0)10/7043478 Fax: +31/(0)10/7043014
amor Gandhi wrote:> > Hi, > > I have gote the following data > > x1 <- c(rep(1,6),rep(4,7),rep(6,10)) > x2 <- rnorm(length(x1),6,1) > data <- data.frame(x1,x2) > > and I would like to compute the mean of the x2 for each individual of x1, > i. e. x1=1,4 and 6? >You'll probably get seven answers to this, but: tapply(x2,list(x1),mean) -- View this message in context: http://www.nabble.com/mean-tp23922878p23922947.html Sent from the R help mailing list archive at Nabble.com.
Hi Amor, I think you forgot to include the individual ID. ?aggregate cheers milton brazil=toronto On Mon, Jun 8, 2009 at 8:11 AM, amor Gandhi <amorigandhi@yahoo.de> wrote:> Hi, > > I have gote the following data > > x1 <- c(rep(1,6),rep(4,7),rep(6,10)) > x2 <- rnorm(length(x1),6,1) > data <- data.frame(x1,x2) > > and I would like to compute the mean of the x2 for each individual of x1, > i. e. x1=1,4 and 6? > > Thank you very much in advance, > > Amori > > > > [[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<http://www.r-project.org/posting-guide.html> > and provide commented, minimal, self-contained, reproducible code. >[[alternative HTML version deleted]]
Many thanks --- milton ruser <milton.ruser@gmail.com> schrieb am Mo, 8.6.2009: Von: milton ruser <milton.ruser@gmail.com> Betreff: Re: [R] mean An: "amor Gandhi" <amorigandhi@yahoo.de> CC: r-help@stat.math.ethz.ch Datum: Montag, 8. Juni 2009, 14:27 oops. If x1 is the individual try x2.mean<-aggregate(data[2:2], list(x1), mean) x2.mean you can change "mean" by any function. cheers milton On Mon, Jun 8, 2009 at 8:20 AM, milton ruser <milton.ruser@gmail.com> wrote: Hi Amor, I think you forgot to include the individual ID. ?aggregate cheers milton brazil=toronto On Mon, Jun 8, 2009 at 8:11 AM, amor Gandhi <amorigandhi@yahoo.de> wrote: Hi, I have gote the following data x1 <- c(rep(1,6),rep(4,7),rep(6,10)) x2 <- rnorm(length(x1),6,1) data <- data.frame(x1,x2) and I would like to compute the mean of the x2 for each individual of x1, i. e. x1=1,4 and 6? Thank you very much in advance, Amori [[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]]