title: populate a new column in a df with new values
body: <p>I'm looking to populate a new data frame column with a calculated value that is unique to each subgroup of data. Here is my exact code:</p>
  
  <pre><code> df &lt;- read.csv('data_30_Mar2015.csv')
  
  
   df$dCT &lt;- NA
  
   #FUNCTION
   calc_dCT &lt;- function(sample, DF){
  
   sample_df &lt;- DF[ which(DF$Sample=='sample'),]
   print (sample_df)
   VIC &lt;- sample_df[ which(sample_df$Reporter=='VIC'),]
   FAM &lt;- sample_df[ which(sample_df$Reporter=='FAM'),]
  
   VIC_mean&lt;-mean(VIC[,3])
   FAM_mean&lt;-mean(FAM[,3])
  
   DCT &lt;- FAM_mean - VIC_mean
  
   for (i in 1:length(sample_df)){
       sample_df[i,4] &lt;- DCT
       }
   DF&lt;-merge(DF, sample_df, all=TRUE)
   }
  
   #CALLS TO FUNCTION
   calc_dCT('c48', df)
   calc_dCT('m48', df)
   calc_dCT('c72', df)
   calc_dCT('m72', df)
  
   print (df)
  </code></pre>
  
  <p>and here is the output:</p>
  
  <pre><code> calc_dCT('c48', df)
   [1] Sample   Reporter CT       dCT     
   &lt;0 rows&gt; (or 0-length row.names)
   calc_dCT('m48', df)
   [1] Sample   Reporter CT       dCT     
   &lt;0 rows&gt; (or 0-length row.names)
   calc_dCT('c72', df)
   [1] Sample   Reporter CT       dCT     
   &lt;0 rows&gt; (or 0-length row.names)
   calc_dCT('m72', df)
   [1] Sample   Reporter CT       dCT     
   &lt;0 rows&gt; (or 0-length row.names)
  
   print (df)
  Sample Reporter       CT dCT
  1     m48      VIC 27.50595  NA
  2     m48      VIC 27.77835  NA
  3     m48      VIC 27.62321  NA
  4     m48      FAM 30.87295  NA
  5     m48      FAM 30.87967  NA
  6     m48      FAM 30.73427  NA
  7     c48      VIC 26.56715  NA
  8     c48      VIC 26.89787  NA
  9     c48      VIC 26.82587  NA
  10    c48      FAM 30.20642  NA
  11    c48      FAM 30.43074  NA
  12    c48      FAM 30.36933  NA
  13    m72      VIC 29.61585  NA
  14    m72      VIC 28.65742  NA
  15    m72      VIC 29.40057  NA
  16    m72      FAM 32.27304  NA
  17    m72      FAM 32.38696  NA
  18    m72      FAM 32.24386  NA
  19    c72      VIC 28.22370  NA
  20    c72      VIC 28.17342  NA
  21    c72      VIC 28.49104  NA
  22    c72      FAM 31.91751  NA
  23    c72      FAM 31.67524  NA
  24    c72      FAM 31.87287  NA
  </code></pre>
  
  <p>It doesn't seem to be subsetting the data correctly and I'm not sure why this would be. I'm trying to populate the column 'dCT' with the calculated value for DCT.</p>