I have a dataframe with names in the first column and wait times between decisions in the second column. Since individuals make multiple decisions, I want the average for each individual. For example, the data might look like this name wtime jo 1 jo 2 jo 1 jo 3 tim 3 tim 2 tim 2 ro 1 ro 2 etc. I'm hoping there is something like mean(dataname$wtime[name]) which will just create a column with length equal to the number of different names (levels) and an average wtime for each. So far though, I haven't had much luck figuring that one out. Thanks. Thomas
?tapply Bendix Carstensen> -----Original Message----- > From: r-help-bounces at stat.math.ethz.ch > [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of thomas hills > Sent: Saturday, February 26, 2005 6:17 PM > To: r-help at stat.math.ethz.ch > Subject: [R] averaging within columns > > > I have a dataframe with names in the first column and wait times > between decisions in the second column. Since individuals make > multiple decisions, I want the average for each individual. For > example, the data might look like this > > name wtime > jo 1 > jo 2 > jo 1 > jo 3 > tim 3 > tim 2 > tim 2 > ro 1 > ro 2 > etc. > > I'm hoping there is something like > > mean(dataname$wtime[name]) > > which will just create a column with length equal to the number of > different names (levels) and an average wtime for each. So > far though, > I haven't had much luck figuring that one out. > > Thanks. > Thomas > > ______________________________________________ > 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 >
Hello Thomas, The function aggregate will do the job. Marc Mamin
Hi,> I'm hoping there is something like > > mean(dataname$wtime[name]) > > which will just create a column with length equal to the number of > different names (levels) and an average wtime for each. So > far though, > I haven't had much luck figuring that one out. >Does the following code do what you want? #### given you have the data like this: name <- c(rep("jo", 4), rep("tim",3), rep("ro", 2)) wtime <- c(1,2,1,3,3,2,2,1,2) mydf <- data.frame(name=name, wtime=wtime) #### this should give you the desired result tapply(X=mydf$wtime, INDEX=list(mydf$name), FUN=mean) Hope this turns out to be useful. Best, Roland +++++ This mail has been sent through the MPI for Demographic Rese...{{dropped}}