Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Replacing all zeroes to NA:</p> <pre><code>df[df == 0] &lt;- NA </code></pre> <p><br><br></p> <p><strong>Explanation</strong></p> <p><strong>1.</strong> It is not <code>NULL</code> what you should want to replace zeroes with. As it says in <code>?'NULL'</code>,</p> <blockquote> <p>NULL represents the null object in R</p> </blockquote> <p>which is unique and, I guess, can be seen as the most uninformative and empty object.<sup>1</sup> Then it becomes not so surprising that</p> <pre><code>data.frame(x = c(1, NULL, 2)) # x # 1 1 # 2 2 </code></pre> <p>That is, R does not reserve any space for this null object.<sup>2</sup> Meanwhile, looking at <code>?'NA'</code> we see that</p> <blockquote> <p>NA is a logical constant of length 1 which contains a missing value indicator. NA can be coerced to any other vector type except raw.</p> </blockquote> <p>Importantly, <code>NA</code> is of length 1 so that R reserves some space for it. E.g.,</p> <pre><code>data.frame(x = c(1, NA, 2)) # x # 1 1 # 2 NA # 3 2 </code></pre> <p>Also, the data frame structure requires all the columns to have the same number of elements so that there can be no "holes" (i.e., <code>NULL</code> values).</p> <p>Now you could replace zeroes by <code>NULL</code> in a data frame in the sense of completely removing all the rows containing at least one zero. When using, e.g., <code>var</code>, <code>cov</code>, or <code>cor</code>, that is actually equivalent to first replacing zeroes with <code>NA</code> and setting the value of <code>use</code> as <code>"complete.obs"</code>. Typically, however, this is unsatisfactory as it leads to extra information loss.</p> <p><strong>2.</strong> Instead of running some sort of loop, in the solution I use <code>df == 0</code> vectorization. <code>df == 0</code> returns (try it) a matrix of the same size as <code>df</code>, with the entries <code>TRUE</code> and <code>FALSE</code>. Further, we are also allowed to pass this matrix to the subsetting <code>[...]</code> (see <code>?'['</code>). Lastly, while the result of <code>df[df == 0]</code> is perfectly intuitive, it may seem strange that <code>df[df == 0] &lt;- NA</code> gives the desired effect. The assignment operator <code>&lt;-</code> is indeed not always so smart and does not work in this way with some other objects, but it does so with data frames; see <code>?'&lt;-'</code>.</p> <p><br></p> <p><sub><sup>1</sup> The empty set in the set theory feels somehow related.</sub><br> <sub><sup>2</sup> Another similarity with the set theory: the empty set is a subset of every set, but we do not reserve any space for it.</sub></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload