Q:

Normalise multiple variables to separate control values

I am interested in normalising multiple variables to the respective mean value of a control subgroup.
Say I have a data frame where I am measuring two variables (Score 1 and Score 2) from 3 different conditions (control, drug 1, drug 2).
df <- data.frame(Treatment=rep(c( "Control", "Drug 1",
    "Drug 2"), each=6 ),
    Score1=c(4,5,4,5,5,6,8,9,10,8,9,9,14,15,13,15,14,15), 
    Score2=c(1,2,1,2,3,3,8,8,9,9,8,8,14,14,15,12,14,15)) 
df

   Treatment Score1 Score2
1    Control      4      1
2    Control      5      2
3    Control      4      1
4    Control      5      2
5    Control      5      3
6    Control      6      3
7     Drug 1      8      8
8     Drug 1      9      8
9     Drug 1     10      9
10    Drug 1      8      9
11    Drug 1      9      8
12    Drug 1      9      8
13    Drug 2     14     14
14    Drug 2     15     14
15    Drug 2     13     15
16    Drug 2     15     12
17    Drug 2     14     14
18    Drug 2     15     15

I would like to normalise each score to the mean value of the control group (for that score). The end result being:
df.normal <- df
x <- mean(df$Score1[df$Treatment=="Control"])
y <- mean(df$Score2[df$Treatment=="Control"])
df.normal$Score1_normalised <- df$Score1 / x
df.normal$Score2_normalised <- df$Score2 / y
df.normal

Treatment Score1 Score2 Score1_normalised Score2_normalised
1    Control      4      1         0.8275862               0.5
2    Control      5      2         1.0344828               1.0
3    Control      4      1         0.8275862               0.5
4    Control      5      2         1.0344828               1.0
5    Control      5      3         1.0344828               1.5
6    Control      6      3         1.2413793               1.5
7     Drug 1      8      8         1.6551724               4.0
8     Drug 1      9      8         1.8620690               4.0
9     Drug 1     10      9         2.0689655               4.5
10    Drug 1      8      9         1.6551724               4.5
11    Drug 1      9      8         1.8620690               4.0
12    Drug 1      9      8         1.8620690               4.0
13    Drug 2     14     14         2.8965517               7.0
14    Drug 2     15     14         3.1034483               7.0
15    Drug 2     13     15         2.6896552               7.5
16    Drug 2     15     12         3.1034483               6.0
17    Drug 2     14     14         2.8965517               7.0
18    Drug 2     15     15         3.1034483               7.5

I think this is possible with dplyr but I've been struggling to get started, and as I have around 20 variables I was hoping for a shortcut rather than going about it the long way.
Any help would be greatly appreciated!

A:

Here is with aggregate() and mapply():
> Medias <- aggregate(df[c("Score1", "Score2")], list(df$Treatment), mean)
> Medias
  Group.1    Score1    Score2
1 Control  4.833333  2.000000
2  Drug 1  8.833333  8.333333
3  Drug 2 14.333333 14.000000
> 
> mapply( function(x, y) {x / y}, x = df[c("Score1", "Score2")], y = Medias[Medias$Group.1 == "Control" , c("Score1", "Score2")])
         Score1 Score2
 [1,] 0.8275862    0.5
 [2,] 1.0344828    1.0
 [3,] 0.8275862    0.5
 [4,] 1.0344828    1.0
 [5,] 1.0344828    1.5
 [6,] 1.2413793    1.5
 [7,] 1.6551724    4.0
 [8,] 1.8620690    4.0
 [9,] 2.0689655    4.5
[10,] 1.6551724    4.5
[11,] 1.8620690    4.0
[12,] 1.8620690    4.0
[13,] 2.8965517    7.0
[14,] 3.1034483    7.0
[15,] 2.6896552    7.5
[16,] 3.1034483    6.0
[17,] 2.8965517    7.0
[18,] 3.1034483    7.5
> 

Hope it helps.

