Hi All, Sorry about this quite basic, but I am very new to R. I have a data file which has a dependent variable (reaction time) and a couple of independent variables, one of which is coded 1-8; I want to calculate the reaction time for each of the 8 codes of the independent variable. Thanks for any help, Laura
Hi Laura, I think you're looking for aggregate() See ?aggregate If you had posted a reproducible example, I could have given you a more detailed answer. Learn to use the dput() function to do so. If you're very new to R, maybe this could help you get started: http://www.burns-stat.com/documents/tutorials/impatient-r/ HTH, Ivan -- Ivan CALANDRA Universit? de Bourgogne UMR CNRS/uB 6282 Biog?osciences 6 Boulevard Gabriel 21000 Dijon, FRANCE ivan.calandra at u-bourgogne.fr http://biogeosciences.u-bourgogne.fr/calandra Le 03/06/13 11:15, Laura Thomas a ?crit :> Hi All, > > Sorry about this quite basic, but I am very new to R. > > I have a data file which has a dependent variable (reaction time) and a couple of independent variables, one of which is coded 1-8; I want to calculate the reaction time for each of the 8 codes of the independent variable. > > Thanks for any help, > > Laura > > ______________________________________________ > 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. >
Hello, Like Ivan said, you should give us a data example, the best way is to do it is to paste the output of ?dput in a post. If your data frame is named 'dat' use the following. dput(head(dat, 50)) # paste the output of this in a post As for the question, here is an example using ?aggregate, with fake data. dat <- data.frame(X = rnorm(100), A = sample(8, 100, TRUE)) aggregate(X ~ A, data = dat, FUN = mean) Hope this helps, Rui Barradas Em 03-06-2013 10:15, Laura Thomas escreveu:> Hi All, > > Sorry about this quite basic, but I am very new to R. > > I have a data file which has a dependent variable (reaction time) and a couple of independent variables, one of which is coded 1-8; I want to calculate the reaction time for each of the 8 codes of the independent variable. > > Thanks for any help, > > Laura > > ______________________________________________ > 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. >
HI,You could also try: set.seed(24) dat1<- data.frame(Reaction_time=sample(24:60,80,replace=TRUE),Categ=rep(1:8,each=10)) with(dat1,tapply(Reaction_time,list(Categ),FUN=mean)) #?? 1??? 2??? 3??? 4??? 5??? 6??? 7??? 8 #43.7 39.8 37.5 42.7 33.9 42.3 43.2 40.0 #or library(plyr) ?ddply(dat1,.(Categ),summarize, RTMean=mean(Reaction_time)) #? Categ RTMean #1???? 1?? 43.7 #2???? 2?? 39.8 #3???? 3?? 37.5 #4???? 4?? 42.7 #5???? 5?? 33.9 #6???? 6?? 42.3 #7???? 7?? 43.2 #8???? 8?? 40.0 A.K. ----- Original Message ----- From: Laura Thomas <skagandbonegirl at hotmail.com> To: r-help at r-project.org Cc: Sent: Monday, June 3, 2013 5:15 AM Subject: [R] Calculating Mean Hi All, Sorry about this quite basic, but I am very new to R. I have a data file which has a dependent variable (reaction time) and a couple of independent variables, one of which is coded 1-8; I want to calculate the reaction time for each of the 8 codes of the independent variable. Thanks for any help, Laura ______________________________________________ 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.