title: Dichotomize data by factor
body: <p>I need to create a dichtomized variable based on two factors (one hopes it's possible). </p>
  
  <p>Let's say I have the data:</p>
  
  <pre><code>    d &lt;- data.frame ( 
      agegroup = c(2,1,1,2,3,2,1,3,3,3,3,3,1,1,2,3,2,1,1,2,1,2,2,3) ,
      gender = c(2,2,2,2,2,2,1,2,1,1,1,2,1,1,2,2,1,1,1,1,2,1,1,1) , 
      hourwalking = c(0.3,0.5,1.1,1.1,1.1,1.2,1.2,1.2,1.3,1.5,1.7,1.8,2.1,2.1,2.2,2.2,2.3,2.4,2.4,3,3.1,3.1,4.3,5)        
      )
  </code></pre>
  
  <p>I would like to create a binary (LowWalkHrs) using the gender- and agegroup-specific median (e.g., when agegroup = 1 and gender = 1, median = 2.1 (median was found using excel)). The LowWalkHrs would be an added variable in the dataset, so the output would be:</p>
  
  <pre><code>     agegroup gender hourwalk LowWalkHrs
          2       2       0.3       1
          1       2       0.5       1
          1       2       1.1       0
          2       2       1.1       1
          3       2       1.1       1
          2       2       1.2       0
          1       1       1.2       1
            ....
          3       1       5         0
  </code></pre>
  
  <p>I have a rather large dataset (~10k observations), so Excel is out of the question. </p>
  
  <p>In R I've tried cut and cut2, which doesn't seem to take factor variables, as well  ddply, which gave me an error message of (Error in <code>$&lt;-.data.frame</code>(<code>*tmp*</code>, "lowWalkHrs", value = list(hourwalking = c(0.63,  :  replacement has 949 rows, data has 11303).</p>