title: how to avoid For loop to calculate new varaible by group
body: <p>I hope someone here could help me. I am trying to learn  R coding for my work. </p>
  
  <p>I am following the growth of plants ( called Ecotype) over the time,  they are Treated with Mock or a bacteria Xcc. I have 2 diffrent experiements (done at different time) and after image processing  i get the Area. </p>
  
  <p>I would like to compute Normalized_Area = Area(t1)/Area(t0)  for each ecotype, for each treatment for each experiment (Manip) wich is te Area at a time divide by the Area of this ecotype à the bigining of the experiment(t0). Each plant have a different Area at time 0 and the different experiments have a different starting time. ( example of expected results in Normalized_Area column)</p>
  
  <p>Please find below a piece of my df</p>
  
  <pre><code>    # A tibble: 24 x 6
     Manip Traitment Ecotype Date                 Area Normalized_Area
     &lt;dbl&gt; &lt;chr&gt;     &lt;chr&gt;   &lt;dttm&gt;              &lt;dbl&gt;           &lt;dbl&gt;
   1     1 mock      a1-2    2017-12-12 00:00:00 17699            1   
   2     1 mock      a1-2    2017-12-13 00:00:00 24538            1.39
   3     1 mock      a1-2    2017-12-14 00:00:00 27958            1.58
   4     1 xcc       a1-2    2017-12-12 00:00:00 19857            1   
   5     1 xcc       a1-2    2017-12-13 00:00:00 27973            1.41
   6     1 xcc       a1-2    2017-12-14 00:00:00 35875            1.81
   7     2 mock      a1-2    2018-03-20 00:00:00 18177            1   
   8     2 mock      a1-2    2018-03-21 00:00:00 20251            1.11
   9     2 mock      a1-2    2018-03-23 00:00:00 36679            2.02
  10     2 xcc       a1-2    2018-03-20 00:00:00 17261            1   
  11     2 xcc       a1-2    2018-03-21 00:00:00 18697            1.08
  12     2 xcc       a1-2    2018-03-23 00:00:00 35345            2.05
  13     1 mock      a1-10   2017-12-12 00:00:00 22853            1   
  14     1 mock      a1-10   2017-12-13 00:00:00 34641            1.52
  15     1 mock      a1-10   2017-12-14 00:00:00 40311            1.76
  16     1 xcc       a1-10   2017-12-12 00:00:00 23754            1   
  17     1 xcc       a1-10   2017-12-13 00:00:00 33247            1.40
  18     1 xcc       a1-10   2017-12-14 00:00:00 40603            1.71
  19     2 mock      a1-10   2018-03-20 00:00:00 28201            1   
  20     2 mock      a1-10   2018-03-21 00:00:00 30306            1.07
  21     2 mock      a1-10   2018-03-23 00:00:00 49086            1.74
  22     2 xcc       a1-10   2018-03-20 00:00:00 27217            1   
  23     2 xcc       a1-10   2018-03-21 00:00:00 29844            1.10
  24     2 xcc       a1-10   2018-03-23 00:00:00 46540            1.71
  </code></pre>
  
  <p>I wrote a piece of code using For loops, but it raise some errors and i would like to turn it into a more readable code with dplyr </p>
  
  <pre><code>date_debut=c("2017-12-12", "2018-03-20") # starting_time
  data$Normalized_Area = NA   
  
  for(manips in levels(as.factor(data$Manip))){     # for each manip      
     for(ecoty in levels(as.factor(data$Ecotype))){  # for each ecotype        
         for(traity in levels(as.factor(data$Traitement))){  # for each treatment           
             for( dd in levels(as.factor(date_debut))){    # for each level 
              tmp = subset(data,subset=c(Traitement==traity &amp; Ecotype == ecoty &amp; Manip == manips))  # creation d'un fichier tmp
  
              if(dim(tmp)[1] != 0){                                            
              #tmp = ordered(tmp$date[1:length(tmp$date-1)])
  
              # compute Area mean at D=0 for each Experiement 
  
                  if(dd %in% as.character(tmp$Date)!=F){            
                A0 = tmp$Area[as.character(tmp$Date)== dd] # Select  A0 in                  tmp$Area corresponding to dd
                Norm_Area = tmp$Area /A0
                data$Normalized_Area[data$Traitement == traity &amp; data$Ecotype== ecoty &amp; data$Manip == manips] = Norm_Area 
              }
          }
        }
      }
    }
  </code></pre>
  
  <p>Here the begining of my new code  but i get stuck  </p>
  
  <pre><code>gpeData %&gt;% 
      group_by(Traitement, Ecotype, Manip )  %&gt;% 
      mutate_( Normalized_Area = Area / Area[wich(Date %in% date_debut)] ) 
  </code></pre>
  
  <p>Does someone have any idea how to do that? I appologize for the uggly code but i learned alone
  Best</p>