Hi:

Assuming I interpreted your intentions correctly, here are three different
ways to get means of Q and T by combinations of dP and n. Your data were
read into a data frame named dd. (Note that there are other ways to do this
as well...)

(1)  aggregate():
with(dd, aggregate(cbind(Q, T) ~ dP + n, data = dd, FUN = mean))
  dP n         Q        T
1 10 1 0.4145455 21.60000
2 20 1 0.8100000 21.60000
3 30 1 1.2100000 21.59444
4 40 1 1.5905556 21.50000
5 50 1 1.8380000 21.50000
6 10 2 0.2033333 22.40000
7 20 2 0.4311111 22.40000
8 30 2 0.6012500 22.40000
9 40 2 0.7700000 22.40000

(2) Function summaryBy() in package doBy:
library(doBy)
summaryBy(Q + T ~ dP + n, data = dd, FUN = mean)
  dP n    Q.mean   T.mean
1 10 1 0.4145455 21.60000
2 10 2 0.2033333 22.40000
3 20 1 0.8100000 21.60000
4 20 2 0.4311111 22.40000
5 30 1 1.2100000 21.59444
6 30 2 0.6012500 22.40000
7 40 1 1.5905556 21.50000
8 40 2 0.7700000 22.40000
9 50 1 1.8380000 21.50000

(3) Function ddply() in package plyr:

library(plyr)
ddply(dd, .(dP, n), summarise, Qavg = mean(Q), Tavg = mean(T))
  dP n      Qavg     Tavg
1 10 1 0.4145455 21.60000
2 10 2 0.2033333 22.40000
3 20 1 0.8100000 21.60000
4 20 2 0.4311111 22.40000
5 30 1 1.2100000 21.59444
6 30 2 0.6012500 22.40000
7 40 1 1.5905556 21.50000
8 40 2 0.7700000 22.40000
9 50 1 1.8380000 21.50000

HTH,
Dennis

On Mon, Aug 16, 2010 at 1:12 AM, Benoit Boulinguiez <
benoit.boulingu...@ensc-rennes.fr> wrote:

> Dear R users,
>
> I seek for a more elegant manner to manipulate my data that that I produced
> so far.
> Data is in a data frame THC515.DATA -sample at the end of this mail- of 5
> variables, a string and 4 continuous numeric variables.
>
> I need to get the mean of two variables "Q" and "T" at the levels of a
> third one "dP", individually for each level of "n".
> I managed my way through it, but it is really "roughly" done. I'd like to
> learn a more elegant manner for that.
> Thanks for your time.
>
> The unaesthetic code I produced so far is as follows:
>
> ############ DATA MANIPULATION
> THC515<-aggregate(subset(THC515.DATA,n==1)$Q
>        ,list(subset(THC515.DATA,n==1)$dP),mean)
> names(THC515)<-c("dP","Q")
> tmp.T<-tapply(subset(THC515.DATA,n==1)$T
>    ,subset(THC515.DATA,n==1)$dP,mean)
> THC515<-cbind(THC515,
>    T=tmp.T
>    ,mat=rep("THC515",length(tmp.T))
>    ,n=rep(1,length(tmp.T))
>    )
>
> layers<-as.numeric(levels(factor(THC515.DATA$n)))
> for(i in 2:length(layers)){
> tmp.THC515<-aggregate(subset(THC515.DATA,n==layers[i])$Q
>        ,list(subset(THC515.DATA,n==layers[i])$dP),mean)
> names(tmp.THC515)<-c("dP","Q")
> tmp.T<-tapply(subset(THC515.DATA,n==layers[i])$T
>    ,subset(THC515.DATA,n==layers[i])$dP,mean)
> tmp.THC515<-cbind(tmp.THC515,
>    T=tmp.T
>    ,mat=rep("THC515",length(tmp.T))
>    ,n=rep(layers[i],length(tmp.T))
>    )
> THC515<-rbind(THC515,tmp.THC515)
> }
> THC515.DATA<-THC515
>
> #Original DATA
>
> mat     n       Q       dP      T
> THC515  1       0.25    10      21.6
> THC515  1       0.26    10      21.6
> THC515  1       0.27    10      21.6
> THC515  1       0.28    10      21.6
> THC515  1       0.29    10      21.6
> THC515  1       0.3     10      21.6
> THC515  1       0.32    10      21.6
> THC515  1       0.34    10      21.6
> THC515  1       0.36    10      21.6
> THC515  1       0.38    10      21.6
> THC515  1       0.4     10      21.6
> THC515  1       0.42    10      21.6
> THC515  1       0.44    10      21.6
> THC515  1       0.46    10      21.6
> THC515  1       0.48    10      21.6
> THC515  1       0.5     10      21.6
> THC515  1       0.52    10      21.6
> THC515  1       0.54    10      21.6
> THC515  1       0.56    10      21.6
> THC515  1       0.57    10      21.6
> THC515  1       0.58    10      21.6
> THC515  1       0.6     10      21.6
> THC515  1       0.62    20      21.6
> THC515  1       0.64    20      21.6
> THC515  1       0.66    20      21.6
> THC515  1       0.68    20      21.6
> THC515  1       0.7     20      21.6
> THC515  1       0.72    20      21.6
> THC515  1       0.74    20      21.6
> THC515  1       0.76    20      21.6
> THC515  1       0.78    20      21.6
> THC515  1       0.8     20      21.6
> THC515  1       0.82    20      21.6
> THC515  1       0.84    20      21.6
> THC515  1       0.86    20      21.6
> THC515  1       0.88    20      21.6
> THC515  1       0.9     20      21.6
> THC515  1       0.92    20      21.6
> THC515  1       0.94    20      21.6
> THC515  1       0.96    20      21.6
> THC515  1       0.98    20      21.6
> THC515  1       1       20      21.6
> THC515  1       1.04    30      21.6
> THC515  1       1.06    30      21.6
> THC515  1       1.08    30      21.6
> THC515  1       1.1     30      21.6
> THC515  1       1.12    30      21.6
> THC515  1       1.14    30      21.6
> THC515  1       1.16    30      21.6
> THC515  1       1.18    30      21.6
> THC515  1       1.2     30      21.6
> THC515  1       1.22    30      21.6
> THC515  1       1.24    30      21.6
> THC515  1       1.26    30      21.6
> THC515  1       1.28    30      21.6
> THC515  1       1.3     30      21.6
> THC515  1       1.32    30      21.6
> THC515  1       1.34    30      21.6
> THC515  1       1.35    30      21.6
> THC515  1       1.39    30      21.5
> THC515  1       1.42    40      21.5
> THC515  1       1.44    40      21.5
> THC515  1       1.46    40      21.5
> THC515  1       1.48    40      21.5
> THC515  1       1.5     40      21.5
> THC515  1       1.52    40      21.5
> THC515  1       1.54    40      21.5
> THC515  1       1.56    40      21.5
> THC515  1       1.58    40      21.5
> THC515  1       1.6     40      21.5
> THC515  1       1.62    40      21.5
> THC515  1       1.64    40      21.5
> THC515  1       1.66    40      21.5
> THC515  1       1.68    40      21.5
> THC515  1       1.7     40      21.5
> THC515  1       1.72    40      21.5
> THC515  1       1.74    40      21.5
> THC515  1       1.77    40      21.5
> THC515  1       1.79    50      21.5
> THC515  1       1.82    50      21.5
> THC515  1       1.84    50      21.5
> THC515  1       1.86    50      21.5
> THC515  1       1.88    50      21.5
> THC515  2       0.13    10      22.4
> THC515  2       0.14    10      22.4
> THC515  2       0.34    10      22.4
> THC515  2       0.35    20      22.4
> THC515  2       0.37    20      22.4
> THC515  2       0.39    20      22.4
> THC515  2       0.41    20      22.4
> THC515  2       0.43    20      22.4
> THC515  2       0.45    20      22.4
> THC515  2       0.47    20      22.4
> THC515  2       0.49    20      22.4
> THC515  2       0.52    20      22.4
> THC515  2       0.53    30      22.4
> THC515  2       0.55    30      22.4
> THC515  2       0.57    30      22.4
> THC515  2       0.59    30      22.4
> THC515  2       0.61    30      22.4
> THC515  2       0.63    30      22.4
> THC515  2       0.65    30      22.4
> THC515  2       0.68    30      22.4
> THC515  2       0.69    40      22.4
> THC515  2       0.71    40      22.4
> THC515  2       0.73    40      22.4
> THC515  2       0.75    40      22.4
> THC515  2       0.77    40      22.4
> THC515  2       0.79    40      22.4
> THC515  2       0.81    40      22.4
> THC515  2       0.83    40      22.4
> THC515  2       0.85    40      22.4
>
>
>
> --
> -------------
> Benoit Boulinguiez
> Ph.D student
> Ecole de Chimie de Rennes (ENSCR) Bureau 1.20
> Equipe CIP UMR CNRS 6226 "Sciences Chimiques de Rennes"
> Avenue du Général Leclerc
> CS 50837
> 35708 Rennes CEDEX 7
> Tel 33 (0)2 23 23 80 83
> Fax 33 (0)2 23 23 81 20
> http://www.ensc-rennes.fr/
>
> ______________________________________________
> 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]]

______________________________________________
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.

Reply via email to