[R] need of elegant data manipulation

Benoit Boulinguiez benoit.boulinguiez at ensc-rennes.fr
Mon Aug 16 10:12:20 CEST 2010


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/



More information about the R-help mailing list