title: How to clean or remove NA values from a dataset without remove the column or row
body: <p>Is any elegant solution to clean a dataframe from NA values without remove the row or column where the NA is?</p>
  
  <p>Example: </p>
  
  <p>Input dataframe</p>
  
  <pre><code>    C1    C2     C3
   R1  A   &lt;NA&gt;  &lt;NA&gt;
   R2 &lt;NA&gt;  A    &lt;NA&gt;
   R3 &lt;NA&gt; &lt;NA&gt;   A
   R4  B   &lt;NA&gt;  &lt;NA&gt;
   R5 &lt;NA&gt;  B    &lt;NA&gt;
   R6 &lt;NA&gt; &lt;NA&gt;  &lt;NA&gt;
   R7  C   &lt;NA&gt;   B
   R8       C    &lt;NA&gt;
   R9            &lt;NA&gt;
   R10           &lt;NA&gt;
   R11            C
  </code></pre>
  
  <p>Output dataframe    </p>
  
  <pre><code>    C1  C2  C3
  R1  A   A   A
  R2  B   B   B
  R3  C   C   C
  </code></pre>
  
  <p>For example, here is a messy dataframe (df1) full of NA values</p>
  
  <pre><code>    A       B       C       D       E       F    G    H    I    J    K
  1 Healthy    &lt;NA&gt;    &lt;NA&gt;    &lt;NA&gt;    &lt;NA&gt;    &lt;NA&gt; &lt;NA&gt; &lt;NA&gt; &lt;NA&gt; &lt;NA&gt; &lt;NA&gt;
  2    &lt;NA&gt; Healthy    &lt;NA&gt;    &lt;NA&gt;    &lt;NA&gt;    &lt;NA&gt; &lt;NA&gt; &lt;NA&gt; &lt;NA&gt; &lt;NA&gt; &lt;NA&gt;
  3    &lt;NA&gt;    &lt;NA&gt; Healthy    &lt;NA&gt;    &lt;NA&gt;    &lt;NA&gt; &lt;NA&gt; &lt;NA&gt; &lt;NA&gt; &lt;NA&gt; &lt;NA&gt;
  4    &lt;NA&gt;    &lt;NA&gt;    &lt;NA&gt; Healthy    &lt;NA&gt;    &lt;NA&gt; &lt;NA&gt; &lt;NA&gt; &lt;NA&gt; &lt;NA&gt; &lt;NA&gt;
  5    &lt;NA&gt;    &lt;NA&gt;    &lt;NA&gt;    &lt;NA&gt; Healthy    &lt;NA&gt; &lt;NA&gt; &lt;NA&gt; &lt;NA&gt; &lt;NA&gt; &lt;NA&gt;
  6    &lt;NA&gt;    &lt;NA&gt;    &lt;NA&gt;    &lt;NA&gt;    &lt;NA&gt; Healthy &lt;NA&gt; &lt;NA&gt; &lt;NA&gt; &lt;NA&gt; &lt;NA&gt;
  </code></pre>
  
  <p>Here is how it should be the dataframe. </p>
  
  <pre><code>   X1        X2        X3      X4        X5        X6        X7      X8      X9       X10       X11
  1 Healthy   Healthy   Healthy Healthy   Healthy   Healthy   Healthy Healthy Healthy   Healthy   Healthy
  2 Healthy   Healthy   Healthy Healthy   Healthy   Healthy   Healthy Healthy Healthy   Healthy   Healthy
  3 Healthy ICDAS_1_2 ICDAS_1_2 Healthy ICDAS_1_2 ICDAS_1_2 ICDAS_1_2 Healthy Healthy ICDAS_1_2 ICDAS_1_2
  4 Healthy   Healthy   Healthy Healthy   Healthy   Healthy   Healthy Healthy Healthy   Healthy   Healthy
  5 Healthy   Healthy   Healthy Healthy   Healthy   Healthy   Healthy Healthy Healthy   Healthy   Healthy
  6 Healthy   Healthy   Healthy Healthy   Healthy   Healthy   Healthy Healthy Healthy   Healthy   Healthy
  </code></pre>
  
  <p><strong>Note that the cell B-2 from the original dataframe now is in the X2-1. So the main issue here is to find the equivalent to "delete the cell and move all the cells up" function from Calc or Excel</strong></p>
  
  <p>All the answers that I found delete all the row or column where the &lt;NA> value was. 
  The way I managed to do it is (and sorry if this is primitive) was to extract only the valid values to a new dataframe: </p>
  
  <p>First. I create an empty dataframe</p>
  
  <pre><code>library("data.table") # required package
  new_dataframe &lt;-  data.frame(matrix("", ncol = 11, nrow = 1400) )
  </code></pre>
  
  <p>Then, I copy every valid value from the old to the new dataframe</p>
  
  <pre><code>new_dataframe$X1 &lt;- df1$A[!is.na(df2$A)]
  new_dataframe$X2 &lt;- df1$B[!is.na(df2$B)]
  new_dataframe$X3 &lt;- df1$C[!is.na(df2$C)]
  </code></pre>
  
  <p>etc</p>
  
  <p>So, my question is: <strong>is any more elegant solution to "clean" a dataframe from NA values?</strong></p>
  
  <p>Any help is greatly appreciated. </p>