title: Replace infinite values in an R data frame [why doesn't `is.infinite()` behave like `is.na()`]
body: <pre><code>library(tidyverse)
  df &lt;- tibble(col1 = c("A", "B", "C"),
               col2 = c(NA, Inf, 5))
  #&gt; # A tibble: 3 x 2
  #&gt;   col1   col2
  #&gt;   &lt;chr&gt; &lt;dbl&gt;
  #&gt; 1 A        NA
  #&gt; 2 B       Inf
  #&gt; 3 C         5
  </code></pre>
  
  <p>I can use the base R <code>is.na()</code> function to easily replace <code>NA</code>s with <code>0</code>s, shown below:</p>
  
  <pre><code>df %&gt;% replace(is.na(.), 0)
  #&gt; # A tibble: 3 x 2
  #&gt;   col1   col2
  #&gt;   &lt;chr&gt; &lt;dbl&gt;
  #&gt; 1 A         0
  #&gt; 2 B       Inf
  #&gt; 3 C         5
  </code></pre>
  
  <p>If I try to duplicate this logic with <code>is.infinite()</code> things break:</p>
  
  <pre><code>df %&gt;% replace(is.infinite(.), 1)
  #&gt; Error in is.infinite(.) : default method not implemented for type 'list'
  </code></pre>
  
  <p>Looking at this <a href="https://stackoverflow.com/questions/12188509/cleaning-inf-values-from-an-r-dataframe">older answer about Inf and R data frames</a> I can hack together the solution shown below. This takes my original data frame and turns all <code>NA</code> into <code>0</code> and all <code>Inf</code> into <code>1</code>. Why doesn't <code>is.infinite()</code> behave like <code>is.na()</code> and what is <em>(perhaps)</em> a better way to do what I want?</p>
  
  <pre><code>df %&gt;% 
    replace(is.na(.), 0) %&gt;% 
    mutate_if(is.numeric, list(~na_if(abs(.), Inf))) %&gt;%  # line 3
    replace(is.na(.), 1)
  #&gt; # A tibble: 3 x 2
  #&gt;   col1   col2
  #&gt;   &lt;chr&gt; &lt;dbl&gt;
  #&gt; 1 A         0
  #&gt; 2 B         1
  #&gt; 3 C         5
  </code></pre>