title: R: SAS (if/then statement )in R
body: <p>I was working previously with SAS and then decided to shift to R for academic requirements reasons.
  My data (healthdemo) are health data containing some health diagnostic codes (ICD-10), I want to separate these codes into different columns. This is part of str(healthdemo):</p>
  
  <pre><code>$ PATIENT_KEY     : int  7391510 7404298 7390196 7381208 7401691 7381223 7383005 10188634 7384574 7398317 ...
   $ ICDCODE         : Factor w/ 1125 levels "","H00","H00.0",..: 654 56 654 654 665 48 90 679 654 654 ...
   $ PATIENT_ID      : int  39387 50244 38388 27346 49922 27901 27867 61527 33186 45309 ...
   $ DATE_OF_BIRTH   : Factor w/ 14801 levels "","01/01/1000",..: 7506 10250 52 73 94 6130 85 2710 95 100 ...
  </code></pre>
  
  <p>the ICDCODE contains many diseases from H00 to J99; first, I separated the letters from numbers in the ICDCODE</p>
  
  <pre><code>healthdemo$icd_char = substr(healthdemo$ICDCODE,1,1)
  healthdemo$icd_num = substr(healthdemo$ICDCODE,2,2)
  </code></pre>
  
  <p>then I created diseases columns by this function:</p>
  
  <pre><code>healthdemo$cvd = 0
  healthdemo$ihd = 0
  healthdemo$mi = 0
  healthdemo$dys = 0
  healthdemo$afib = 0
  healthdemo$chf = 0
  </code></pre>
  
  <p>now I want to apply a function similar to this SAS function (that I used to use):</p>
  
  <pre><code>if icd_char = 'I' and 01 &lt;= icd_num &lt; 52 then cvd = 1;
  
  if icd_char = 'I' and 20 &lt;= icd_num &lt;= 25 then ihd = 1;
  
  if icd_char = 'I' and 21 &lt;= icd_num &lt;= 22 then mi = 1;
  
  if icd_char = 'I' and 46 &lt;= icd_num &lt;= 49 then dys = 1;
  
  if icd_char = 'I' and icd_num = 48 then afib = 1;
  </code></pre>
  
  <p>this function will assign each patient with the given ICD character and ICD-number into cvd=1 (e.g.) and so on. </p>
  
  <p>I tried using these functions in R but they didnt work for me:</p>
  
  <pre><code>healthdemo$cvd[healthdemo$icd_char == 'I' &amp; 01 &lt;= healthdemo$icd_num 
        &amp; healthdemo$icd_num &lt; 52 ] &lt;- 1
  </code></pre>
  
  <p>and this </p>
  
  <pre><code>if (healthdemo$icd_char == "I" &amp;  01 &lt; = healthdemo$icd_num &lt; 52  )
     {healthdemo$cvd &lt;- 1} 
  </code></pre>
  
  <p>Would somebody help me please ?</p>