title: Error in `$<-.data.frame`(`*tmp*`, Diabetes, value = c(0, 0, 0, 0, 0, : replacement has 21 rows, data has 101
body: <p>I'm learning R in an online course and whenever I try to input the following (as suggested by the course), I keep getting this error:</p>
  
  <pre><code>&gt; COPD$Diabetes&lt;-c(0, 1)[as.integer(COPD$Diabetes)]
  Error in `$&lt;-.data.frame`(`*tmp*`, Diabetes, value = c(0, 0, 0, 0, 0,  : 
    replacement has 21 rows, data has 101
  </code></pre>
  
  <p>I have the feeling that I'm missing something obvious. Basically the point is to convert COPD$Diabetes into an integer (instead of a factor, so that it can be fitted into a multiple regression model) and I have been able to do that with another method but I would like to know why they suggest the above method and why it isn't working for me?</p>
  
  <p>What is the point of c(0, 1) in this case? I played around with c(0,1)[ ] and using it here makes no sense to me seeing as Diabetes is also a list of integers containing 0 and 1:</p>
  
  <pre><code>&gt; data&lt;-c(1, 0, 1, 0)
  &gt; class(data)
  [1] "numeric"
  &gt; c(0,1)[as.integer(c(1,0,1,0))]
  [1] 0 0
  &gt; c(0,1)[as.integer(c(0,0,0,0))]
  numeric(0)
  &gt; c(0,1)[as.integer(c(1,1,1,1))]
  [1] 0 0 0 0
  </code></pre>
  
  <p>I've been reading so many online tutorials and browsing similar questions but am still stuck. I included some code below for some background info regarding the dataset:</p>
  
  <pre><code>&gt; str(COPD)
  'data.frame':   101 obs. of  24 variables:
   $ X           : int  1 2 3 4 5 6 7 8 9 10 ...
   $ ID          : int  58 57 62 145 136 84 93 27 114 152 ...
   $ AGE         : int  77 79 80 56 65 67 67 83 72 75 ...
   $ PackHistory : num  60 50 11 60 68 26 50 90 50 6 ...
   $ COPDSEVERITY: Factor w/ 4 levels "MILD","MODERATE",..: 3 2 2 4 3 2 3 3 2 3 ...
   $ MWT1        : int  120 165 201 210 204 216 214 214 231 226 ...
   $ MWT2        : int  120 176 180 210 210 180 237 237 237 240 ...
   $ MWT1Best    : int  120 176 201 210 210 216 237 237 237 240 ...
   $ FEV1        : num  1.21 1.09 1.52 0.47 1.07 1.09 0.69 0.68 2.13 1.06 ...
   $ FEV1PRED    : num  36 56 68 14 42 50 35 32 63 46 ...
   $ FVC         : num  2.4 1.64 2.3 1.14 2.91 1.99 1.31 2.23 4.38 2.06 ...
   $ FVCPRED     : int  98 65 86 27 98 60 48 77 80 75 ...
   $ CAT         : int  25 12 22 28 32 29 29 22 25 31 ...
   $ HAD         : num  8 21 18 26 18 21 30 2 6 20 ...
   $ SGRQ        : num  69.5 44.2 44.1 62 75.6 ...
   $ AGEquartiles: int  4 4 4 1 1 2 2 4 3 3 ...
   $ copd        : int  3 2 2 4 3 2 3 3 2 3 ...
   $ gender      : Factor w/ 2 levels "0","1": 2 1 1 2 2 1 1 2 2 1 ...
   $ smoking     : int  2 2 2 2 2 1 1 2 1 2 ...
   $ Diabetes    : int  1 1 1 0 0 1 1 1 1 0 ...
   $ muscular    : int  0 0 0 0 1 0 0 0 0 1 ...
   $ hypertension: int  0 0 0 1 1 0 0 0 0 0 ...
   $ AtrialFib   : int  1 1 1 1 0 1 1 1 1 0 ...
   $ IHD         : int  0 1 0 0 0 0 0 0 0 0 ...
  </code></pre>