title: Generate random missing values in a dataframe using R
body: <p>I had a simple example of a dataframe as follows:</p>
  
  <pre><code>a    b   c
  
  10  50  100
  
  45  36  27
  
  15  80  90
  </code></pre>
  
  <p>The output expected to be as shown below.</p>
  
  <pre><code>a   b   c
  
  10  NA  100
  
  NA  NA  NA
  
  15  80  27
  </code></pre>
  
  <p>The programming that I tried as follows:</p>
  
  <pre><code>insert_nas &lt;- function(x) {
  
    len &lt;- length(x)
  
    n &lt;- sample(1:floor((0.01*(dim(x)[1]))), 1)
  
    i &lt;- sample(1:len, n)
  
    x[i] &lt;- NA 
  
    x
  
  }
  
  
  &gt; sapply(incomplete.data,insert_nas)
  
  Error in 1:floor((0.01 * (dim(x)[1]))) : argument of length 0
  </code></pre>
  
  <p>However, there was an error showed up.<br>
  How can I generate random missing values with 1% of missing in the dataframe?</p>