# Centering data frame by factor

6 messages
Open this post in threaded view
|

## Centering data frame by factor

 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
Open this post in threaded view
|

## Re: Centering data frame by factor

 P1-tapply(P1,Experiment,mean)[Experiment] HTH, Daniel ronny wrote 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
Open this post in threaded view
|

## Re: Centering data frame by factor

Open this post in threaded view
|

## Re: Centering data frame by factor

 In reply to this post by Daniel Malter On Jul 19, 2011, at 4:50 AM, Daniel Malter wrote: > > P1-tapply(P1,Experiment,mean)[Experiment] Another way would be with ave(), but I discovered that it does not   accept subsidiary arguments and does not issue warnings either, so   this works:  >  with(dfrm, ave(P1, Experiment, FUN=function(x) scale(x,   scale=FALSE) ) ) [1] -2  0  2  1 -1  0 But this doesn't behave "as directed" ... by my pre-operational R-brain. with(dfrm, ave(P1, Experiment, FUN=scale,  scale=FALSE) ) [1] -1  0  1  1 -1  0 (It applies both default arguments and issues no warning about unused   argument. Most (well, some anyway) functions like this accept   subsidiary arguments with ..., but `ave` uses that construction to   gather its factor arguments rather than expecting them to be in a list   or vector, as do tapply, aggregate, and by. Some functions like mapply   and many other give you a moreArgs option, but not ave.) -- David. > > HTH, > Daniel > > > ronny wrote: >> >> 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-tp3677609p3677620.html> Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > [hidden email] 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. David Winsemius, MD West Hartford, CT ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.