title: Round numbers at a threshold value in R
body: <p>I'm trying to do logical regression and I got to the point where I have the probability for each observation. Now I would like to classify the probabilities to either 0 or 1 given a threshold value</p>
  
  <p>For example, if I have two numbers 0.65 and 0.87 and my threshold is 0.7, I'd like to round 0.65 to 0 and 0.87 to 1.</p>
  
  <p>To achieve this, I've tried the following code which I think is too much for such a simple task, and I'd like to know if there's any function dedicated to perform this.</p>
  
  <pre><code>library(tidyverse)
  
  # create a table of probabilities and predictions (0 or 1)
  df &lt;- tibble(
    prob = runif(20),
    pred = round(prob) # threshold = 0.5
  )
  
  # threshold function for length = 1
  threshold_1 &lt;- function(p,t) {
    if (p &gt; t) 1 else 0
  }
  
  # threshold function for length = p
  threshold_p &lt;- function(ps, t) {
    map2_dbl(ps, t, threshold_1)
  }
  
  # below works.
  df %&gt;% mutate(
    pred = threshold_p(df$prob, 0.7)
  )
  </code></pre>
  
  <p>I've also tried this</p>
  
  <pre><code># threshold = 0.7
  df %&gt;%
    mutate(
    pred = round(prob - 0.2) # threshold = 0.7
  )
  </code></pre>
  
  <p>Above works quite nicely as no probability will be exactly 0 or 1 (as long as we're dealing with distribution functions), so even if I +/- 0.5 to the numbers (to change the threshold value), they will never round to -1 or 2. But it's just that it's not very elegant.</p>
  
  <p>I'd like to know if there is any function that does this in a much simpler way?</p>