Dear all, I have a dataset Exp name id x1 x2 x3 x4 x5 1 a 1 23 24 23 22 30 1 b 2 25 26 27 23 24 1 c 3 32 19 23 25 28 2 a 4 28 32 24 26 27 2 b 5 23 24 25 26 28 2 c 6 23 31 30 38 23 3 a 7 24 25 31 27 29 3 b 8 28 25 26 32 28 3 c 9 21 31 28 23 29 where each name a,b and c is repeated three times. Now I want to calculate the the averages of a,b and c over the three experiments for each x. For example for x1 I want to calculate the average of a like [a(from exp1) + a(exp2)+ a(exp3)]/3= (23+28+24)/3 How can I do that ? Thanks for your help, Regards, Antonia
try something like the following: sapply(split(dat[paste("x", 1:5, sep = "")], dat$name), mean) Best, Dimitris ---- Dimitris Rizopoulos Ph.D. Student Biostatistical Centre School of Public Health Catholic University of Leuven Address: Kapucijnenvoer 35, Leuven, Belgium Tel: +32/(0)16/336899 Fax: +32/(0)16/337015 Web: http://med.kuleuven.be/biostat/ http://www.student.kuleuven.be/~m0390867/dimitris.htm ----- Original Message ----- From: <antoniababe at yahoo.se> To: <r-help at stat.math.ethz.ch> Sent: Wednesday, October 18, 2006 11:10 AM Subject: [R] new R-user needs help> Dear all, > > I have a dataset > > Exp name id x1 x2 x3 x4 x5 > 1 a 1 23 24 23 22 30 > 1 b 2 25 26 27 23 24 > 1 c 3 32 19 23 25 28 > 2 a 4 28 32 24 26 27 > 2 b 5 23 24 25 26 28 > 2 c 6 23 31 30 38 23 > 3 a 7 24 25 31 27 29 > 3 b 8 28 25 26 32 28 > 3 c 9 21 31 28 23 29 > > where each name a,b and c is repeated three times. > Now I want to calculate the the averages of a,b and c > over the three experiments for each x. For example for > x1 I want to calculate the average of a like > > [a(from exp1) + a(exp2)+ a(exp3)]/3= (23+28+24)/3 > > How can I do that ? > > Thanks for your help, > Regards, > > Antonia > > ______________________________________________ > R-help at stat.math.ethz.ch 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. >Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm
Hi, you can use aggregate. aggregate(data[,c(3:8)],list(data$name),mean) should give you what you want. Ciao, Stefano On Wed, Oct 18, 2006 at 11:10:04AM +0200, antoniababe at yahoo.se wrote: <antoniababe>Dear all, <antoniababe> <antoniababe>I have a dataset <antoniababe> <antoniababe>Exp name id x1 x2 x3 x4 x5 <antoniababe>1 a 1 23 24 23 22 30 <antoniababe>1 b 2 25 26 27 23 24 <antoniababe>1 c 3 32 19 23 25 28 <antoniababe>2 a 4 28 32 24 26 27 <antoniababe>2 b 5 23 24 25 26 28 <antoniababe>2 c 6 23 31 30 38 23 <antoniababe>3 a 7 24 25 31 27 29 <antoniababe>3 b 8 28 25 26 32 28 <antoniababe>3 c 9 21 31 28 23 29 <antoniababe> <antoniababe>where each name a,b and c is repeated three times. <antoniababe>Now I want to calculate the the averages of a,b and c <antoniababe>over the three experiments for each x. For example for <antoniababe>x1 I want to calculate the average of a like <antoniababe> <antoniababe>[a(from exp1) + a(exp2)+ a(exp3)]/3= (23+28+24)/3 <antoniababe> <antoniababe>How can I do that ? <antoniababe> <antoniababe>Thanks for your help, <antoniababe>Regards, <antoniababe> <antoniababe>Antonia <antoniababe> <antoniababe>______________________________________________ <antoniababe>R-help at stat.math.ethz.ch mailing list <antoniababe>https://stat.ethz.ch/mailman/listinfo/r-help <antoniababe>PLEASE do read the posting guide http://www.R-project.org/posting-guide.html <antoniababe>and provide commented, minimal, self-contained, reproducible code.
antoniababe at yahoo.se wrote:> Dear all, > > I have a dataset > > Exp name id x1 x2 x3 x4 x5 > 1 a 1 23 24 23 22 30 > 1 b 2 25 26 27 23 24 > 1 c 3 32 19 23 25 28 > 2 a 4 28 32 24 26 27 > 2 b 5 23 24 25 26 28 > 2 c 6 23 31 30 38 23 > 3 a 7 24 25 31 27 29 > 3 b 8 28 25 26 32 28 > 3 c 9 21 31 28 23 29 > > where each name a,b and c is repeated three times. > Now I want to calculate the the averages of a,b and c > over the three experiments for each x. For example for > x1 I want to calculate the average of a like > > [a(from exp1) + a(exp2)+ a(exp3)]/3= (23+28+24)/3 > > How can I do that ?library(prettyR) antonia.df<-read.csv("../antonia.dat") brkdn(x1~name,antonia.df) Jim
try this, if your data frame name is "tab" mean.tab = data.frame (meanA = mean(tab[tab$name == 'a' ,4:8]), meanB = mean(tab[tab$name == 'b',4:8]), meanC = mean(tab[tab$name = 'c',4:8])) mean.tab good luck Romain Romain Lorrilli?re UMR 8079 Laboratoire Ecologie, Syst?matique et Evolution B?t. 362 Universit? Paris-Sud 91405 Orsay cedex France tel : 01 69 15 56 85 fax : 01 69 15 56 96 mobile : 06 81 70 90 70 email : romain.lorrilliere at ese.u-psud.fr [1]antoniababe at yahoo.se a ?crit : Dear all, I have a dataset Exp name id x1 x2 x3 x4 x5 1 a 1 23 24 23 22 30 1 b 2 25 26 27 23 24 1 c 3 32 19 23 25 28 2 a 4 28 32 24 26 27 2 b 5 23 24 25 26 28 2 c 6 23 31 30 38 23 3 a 7 24 25 31 27 29 3 b 8 28 25 26 32 28 3 c 9 21 31 28 23 29 where each name a,b and c is repeated three times. Now I want to calculate the the averages of a,b and c over the three experiments for each x. For example for x1 I want to calculate the average of a like [a(from exp1) + a(exp2)+ a(exp3)]/3= (23+28+24)/3 How can I do that ? Thanks for your help, Regards, Antonia ______________________________________________ [2]R-help at stat.math.ethz.ch mailing list [3]https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide [4]http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. References 1. mailto:antoniababe at yahoo.se 2. mailto:R-help at stat.math.ethz.ch 3. https://stat.ethz.ch/mailman/listinfo/r-help 4. http://www.R-project.org/posting-guide.html
Hi, use aggregate aggregate(dataset[,-c(1:3)],list(dataset$name),mean) should give you what you need Ciao, Sfetano On Wed, Oct 18, 2006 at 11:10:04AM +0200, antoniababe a yahoo.se wrote: <antoniababe>Dear all, <antoniababe> <antoniababe>I have a dataset <antoniababe> <antoniababe>Exp name id x1 x2 x3 x4 x5 <antoniababe>1 a 1 23 24 23 22 30 <antoniababe>1 b 2 25 26 27 23 24 <antoniababe>1 c 3 32 19 23 25 28 <antoniababe>2 a 4 28 32 24 26 27 <antoniababe>2 b 5 23 24 25 26 28 <antoniababe>2 c 6 23 31 30 38 23 <antoniababe>3 a 7 24 25 31 27 29 <antoniababe>3 b 8 28 25 26 32 28 <antoniababe>3 c 9 21 31 28 23 29 <antoniababe> <antoniababe>where each name a,b and c is repeated three times. <antoniababe>Now I want to calculate the the averages of a,b and c <antoniababe>over the three experiments for each x. For example for <antoniababe>x1 I want to calculate the average of a like <antoniababe> <antoniababe>[a(from exp1) + a(exp2)+ a(exp3)]/3= (23+28+24)/3 <antoniababe> <antoniababe>How can I do that ? <antoniababe> <antoniababe>Thanks for your help, <antoniababe>Regards, <antoniababe> <antoniababe>Antonia <antoniababe> <antoniababe>______________________________________________ <antoniababe>R-help a stat.math.ethz.ch mailing list <antoniababe>https://stat.ethz.ch/mailman/listinfo/r-help <antoniababe>PLEASE do read the posting guide http://www.R-project.org/posting-guide.html <antoniababe>and provide commented, minimal, self-contained, reproducible code.
On 18-Oct-06 antoniababe at yahoo.se wrote:> Dear all, > > I have a dataset > > Exp name id x1 x2 x3 x4 x5 > 1 a 1 23 24 23 22 30 > 1 b 2 25 26 27 23 24 > 1 c 3 32 19 23 25 28 > 2 a 4 28 32 24 26 27 > 2 b 5 23 24 25 26 28 > 2 c 6 23 31 30 38 23 > 3 a 7 24 25 31 27 29 > 3 b 8 28 25 26 32 28 > 3 c 9 21 31 28 23 29 > > where each name a,b and c is repeated three times. > Now I want to calculate the the averages of a,b and c > over the three experiments for each x. For example for > x1 I want to calculate the average of a like > > [a(from exp1) + a(exp2)+ a(exp3)]/3= (23+28+24)/3 > > How can I do that ?The solution given by Dimitris may seem mysterious fo a "new R-user". More transparent is the following kind of thing. Suppose (as in Dimitris' solution) your data are in a dataframe called "dat". Then, for example, mean(dat$x1[dat$name=="a"]) [1] 25 which selects the values of x1 for which the name is "a". You can similarly cycle through all the variables x1,...,x5: for(i in (4:8)){print(mean(dat[(name=="a"),i]))} [1] 25 [1] 27 [1] 26 [1] 25 [1] 28.66667 where dat[(name=="a"),i] selects rows of 'dat' according to the value of 'name' and columns according to values of i (x1: i=4, ... ,x5: i=8). This more direct and transparent way of defining access to different elements of the dataframe makes it much easier to carry out different kinds of calculation. Hoping this helps, Ted. -------------------------------------------------------------------- E-Mail: (Ted Harding) <Ted.Harding at nessie.mcc.ac.uk> Fax-to-email: +44 (0)870 094 0861 Date: 18-Oct-06 Time: 12:17:20 ------------------------------ XFMail ------------------------------
Apparently Analagous Threads
- Aggregating a data frame (was: Re: new R-user needs help)
- what's wrong with my simulation programs on logistic regression
- using nested ifelse and rowSums to create new variable?
- how to the p-values or t-values from the lm's results
- Tabulating Sparse Contingency Table