Hi, I have the simulation results of the following structure: run par measured 1 10 12 2 10 14 1 20 20 2 20 26 Where "run" is the simulation run number, "par" is the parameter of the simulation, and "measured" is the value measured in the simulation. This is only a simple example of my results. There are many values measured and many parameters. But the basic structure stays the same: there are many runs (identified by the run number) for the same values of the parameters with various measured values -- they constitute a sample. I would like to calculate the mean of the "measured" value for a sample, and so I would like to obtain the output as follows: par mean 10 13 20 23 I would appreciate it if someone could write me how to do it. Thank you, Irek
On 17.01.2012 12:31, Irek Szczesniak wrote:> Hi, > > I have the simulation results of the following structure: > > run par measured > 1 10 12 > 2 10 14 > 1 20 20 > 2 20 26 > > Where "run" is the simulation run number, "par" is the parameter of > the simulation, and "measured" is the value measured in the > simulation. This is only a simple example of my results. There are > many values measured and many parameters. But the basic structure > stays the same: there are many runs (identified by the run number) for > the same values of the parameters with various measured values -- they > constitute a sample. > > I would like to calculate the mean of the "measured" value for a > sample, and so I would like to obtain the output as follows: > > par mean > 10 13 > 20 23 > > I would appreciate it if someone could write me how to do it.For you data in a data.frame called dat: aggregate(measured ~ par, dat, mean) Uwe Ligges> > Thank you, > Irek > > ______________________________________________ > 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.
Thank you, Uwe, for your help! I have more measurements (m1, m2) and more parameters (par1, par2). I can calculate the means of m1 and m2 this way: aggregate(cbind(m1, m2) ~ par1 + par2, dat, mean) However, I also need to calculate the standard error of the mean, and the variance for the sample, and I would like to have them output as extra columns next to the column with means. Again, I would appreciate any help! On 17.01.2012 15:09, Uwe Ligges wrote:> > > On 17.01.2012 12:31, Irek Szczesniak wrote: >> Hi, >> >> I have the simulation results of the following structure: >> >> run par measured >> 1 10 12 >> 2 10 14 >> 1 20 20 >> 2 20 26 >> >> Where "run" is the simulation run number, "par" is the parameter of >> the simulation, and "measured" is the value measured in the >> simulation. This is only a simple example of my results. There are >> many values measured and many parameters. But the basic structure >> stays the same: there are many runs (identified by the run number) for >> the same values of the parameters with various measured values -- they >> constitute a sample. >> >> I would like to calculate the mean of the "measured" value for a >> sample, and so I would like to obtain the output as follows: >> >> par mean >> 10 13 >> 20 23 >> >> I would appreciate it if someone could write me how to do it. > > > For you data in a data.frame called dat: > > aggregate(measured ~ par, dat, mean) > > Uwe Ligges > > >> >> Thank you, >> Irek >> >> ______________________________________________ >> 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. >-- Ireneusz (Irek) Szczesniak http://www.irkos.org
Try using the function in the plyr package. E.g., > z <- data.frame( # your toy dataset run = c(1, 2, 1, 2), par = c(10, 10, 20, 20), measured = c(12, 14, 20, 26)) > library(plyr) > ddply(z, .(par), summarize, meanMeasured=mean(measured), sdMeasured=sd(measured)) par meanMeasured sdMeasured 1 10 13 1.414214 2 20 23 4.242641 Bill Dunlap Spotfire, TIBCO Software wdunlap tibco.com> -----Original Message----- > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Ireneusz > Szczesniak > Sent: Tuesday, January 17, 2012 2:43 PM > To: r-help at r-project.org > Subject: Re: [R] Mean of simulation runs given in a table > > Thank you, Uwe, for your help! I have more measurements (m1, m2) and > more parameters (par1, par2). I can calculate the means of m1 and m2 > this way: > > aggregate(cbind(m1, m2) ~ par1 + par2, dat, mean) > > However, I also need to calculate the standard error of the mean, and > the variance for the sample, and I would like to have them output as > extra columns next to the column with means. > > Again, I would appreciate any help! > > On 17.01.2012 15:09, Uwe Ligges wrote: > > > > > > On 17.01.2012 12:31, Irek Szczesniak wrote: > >> Hi, > >> > >> I have the simulation results of the following structure: > >> > >> run par measured > >> 1 10 12 > >> 2 10 14 > >> 1 20 20 > >> 2 20 26 > >> > >> Where "run" is the simulation run number, "par" is the parameter of > >> the simulation, and "measured" is the value measured in the > >> simulation. This is only a simple example of my results. There are > >> many values measured and many parameters. But the basic structure > >> stays the same: there are many runs (identified by the run number) for > >> the same values of the parameters with various measured values -- they > >> constitute a sample. > >> > >> I would like to calculate the mean of the "measured" value for a > >> sample, and so I would like to obtain the output as follows: > >> > >> par mean > >> 10 13 > >> 20 23 > >> > >> I would appreciate it if someone could write me how to do it. > > > > > > For you data in a data.frame called dat: > > > > aggregate(measured ~ par, dat, mean) > > > > Uwe Ligges > > > > > >> > >> Thank you, > >> Irek > >> > >> ______________________________________________ > >> 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. > > > > > -- > Ireneusz (Irek) Szczesniak > http://www.irkos.org > > ______________________________________________ > 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.
Thank you, William, for your help! It works great. My final call looks like this: pars <- c(.(nodes), .(load), .(buffer), .(deflections)) ddply(i, pars, summarize, mm_created = mean(mean_created), ms_created = mean(sdev_created), mm_admitted = mean(mean_admitted), ms_admitted = mean(sdev_admitted), mm_dropped = mean(mean_dropped), ms_dropped = mean(sdev_dropped), mm_delivered = mean(mean_delivered), ms_delivered = mean(sdev_delivered)) 2012/1/18 William Dunlap <wdunlap at tibco.com>:> Try using the function in the plyr package. ?E.g., > ?> z <- data.frame( # your toy dataset > ? ? ? run = c(1, 2, 1, 2), > ? ? ? par = c(10, 10, 20, 20), > ? ? ? measured = c(12, 14, 20, 26)) > ?> library(plyr) > ?> ddply(z, .(par), summarize, meanMeasured=mean(measured), sdMeasured=sd(measured)) > ? ?par meanMeasured sdMeasured > ?1 ?10 ? ? ? ? ? 13 1.414214 > ?2 ?20 ? ? ? ? ? 23 4.242641 > > Bill Dunlap > Spotfire, TIBCO Software > wdunlap tibco.com > >> -----Original Message----- >> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Ireneusz >> Szczesniak >> Sent: Tuesday, January 17, 2012 2:43 PM >> To: r-help at r-project.org >> Subject: Re: [R] Mean of simulation runs given in a table >> >> Thank you, Uwe, for your help! ?I have more measurements (m1, m2) and >> more parameters (par1, par2). ?I can calculate the means of m1 and m2 >> this way: >> >> aggregate(cbind(m1, m2) ~ par1 + par2, dat, mean) >> >> However, I also need to calculate the standard error of the mean, and >> the variance for the sample, and I would like to have them output as >> extra columns next to the column with means. >> >> Again, I would appreciate any help! >> >> On 17.01.2012 15:09, Uwe Ligges wrote: >> > >> > >> > On 17.01.2012 12:31, Irek Szczesniak wrote: >> >> Hi, >> >> >> >> I have the simulation results of the following structure: >> >> >> >> run par measured >> >> 1 10 12 >> >> 2 10 14 >> >> 1 20 20 >> >> 2 20 26 >> >> >> >> Where "run" is the simulation run number, "par" is the parameter of >> >> the simulation, and "measured" is the value measured in the >> >> simulation. This is only a simple example of my results. There are >> >> many values measured and many parameters. But the basic structure >> >> stays the same: there are many runs (identified by the run number) for >> >> the same values of the parameters with various measured values -- they >> >> constitute a sample. >> >> >> >> I would like to calculate the mean of the "measured" value for a >> >> sample, and so I would like to obtain the output as follows: >> >> >> >> par mean >> >> 10 13 >> >> 20 23 >> >> >> >> I would appreciate it if someone could write me how to do it. >> > >> > >> > For you data in a data.frame called dat: >> > >> > aggregate(measured ~ par, dat, mean) >> > >> > Uwe Ligges >> > >> > >> >> >> >> Thank you, >> >> Irek >> >> >> >> ______________________________________________ >> >> 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. >> > >> >> >> -- >> Ireneusz (Irek) Szczesniak >> http://www.irkos.org >> >> ______________________________________________ >> 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.