Hi,

I would like to center P1 and P2 of the following data frame by the factor
"Experiment", i.e. substruct from each value the average of its experiment,
and keep the original data structure, i.e. the experiment and the group of
each value. 

RAW=
data.frame("Experiment"=c(2,2,2,1,1,1),"Group"=c("A","A","B","A","A","B"),"P1"=c(10,12,14,5,3,4),"P2"=c(8,12,16,2,3,4))

Desired result:

NORMALIZED=
data.frame("Experiment"=c(2,2,2,1,1,1),"Group"=c("B","A","B","B","A","B"),"P1"=c(-2,0,2,1,-1,0),"P2"=c(-4,0,4,-1,0,1))

I tried using "by", but then I lose the original order, and the "Group"
varaible. Can you help?

> RAW 
  Experiment Group P1 P2
         2     A 10  8
         2     A 12 12
         2     B 14 16
         1     A  5  2
         1     A  3  3
         1     B  4  4

NOT.OK<- within (RAW,
{P1<-do.call(rbind,by(RAW$P1,RAW$Experiment,scale,scale=F))})

> NOT.OK
  Experiment Group P1 P2
          2     A  1  8
          2     A -1 12
          2     B  0 16
          1     A -2  2
          1     A  0  3
          1     B  2  4


--
View this message in context: 
http://r.789695.n4.nabble.com/Centering-data-frame-by-factor-tp3677609p3677609.html
Sent from the R help mailing list archive at Nabble.com.

______________________________________________
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