EVOLUTION-MANAGER
Edit File: fifelse.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><title>R: Fast ifelse</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <link rel="stylesheet" type="text/css" href="R.css" /> </head><body> <table width="100%" summary="page for fifelse {data.table}"><tr><td>fifelse {data.table}</td><td style="text-align: right;">R Documentation</td></tr></table> <h2> Fast ifelse </h2> <h3>Description</h3> <p><code>fifelse</code> is a faster and more robust replacement of <code><a href="../../base/html/ifelse.html">ifelse</a></code>. It is comparable to <code>dplyr::if_else</code> and <code>hutils::if_else</code>. It returns a value with the same length as <code>test</code> filled with corresponding values from <code>yes</code>, <code>no</code> or eventually <code>na</code>, depending on <code>test</code>. Supports <code>bit64</code>'s <code>integer64</code> and <code>nanotime</code> classes. </p> <h3>Usage</h3> <pre> fifelse(test, yes, no, na=NA) </pre> <h3>Arguments</h3> <table summary="R argblock"> <tr valign="top"><td><code>test</code></td> <td> <p> A logical vector. </p> </td></tr> <tr valign="top"><td><code>yes, no</code></td> <td> <p> Values to return depending on <code>TRUE</code>/<code>FALSE</code> element of <code>test</code>. They must be the same type and be either length <code>1</code> or the same length of <code>test</code>. </p> </td></tr> <tr valign="top"><td><code>na</code></td> <td> <p> Value to return if an element of <code>test</code> is <code>NA</code>. It must be the same type as <code>yes</code> and <code>no</code> and length <code>1</code>. Default value <code>NA</code>. <code>NULL</code> is treated as <code>NA</code>. </p> </td></tr> </table> <h3>Details</h3> <p>In contrast to <code><a href="../../base/html/ifelse.html">ifelse</a></code> attributes are copied from <code>yes</code> to the output. This is useful when returning <code>Date</code>, <code>factor</code> or other classes. </p> <h3>Value</h3> <p>A vector of the same length as <code>test</code> and attributes as <code>yes</code>. Data values are taken from the values of <code>yes</code> and <code>no</code>, eventually <code>na</code>. </p> <h3>See Also</h3> <p><code><a href="coalesce.html">fcoalesce</a></code> </p> <h3>Examples</h3> <pre> x = c(1:4, 3:2, 1:4) fifelse(x > 2L, x, x - 1L) # unlike ifelse, fifelse preserves attributes, taken from the 'yes' argument dates = as.Date(c("2011-01-01","2011-01-02","2011-01-03","2011-01-04","2011-01-05")) ifelse(dates == "2011-01-01", dates - 1, dates) fifelse(dates == "2011-01-01", dates - 1, dates) yes = factor(c("a","b","c")) no = yes[1L] ifelse(c(TRUE,FALSE,TRUE), yes, no) fifelse(c(TRUE,FALSE,TRUE), yes, no) # Example of using the 'na' argument fifelse(test = c(-5L:5L < 0L, NA), yes = 1L, no = 0L, na = 2L) </pre> <hr /><div style="text-align: center;">[Package <em>data.table</em> version 1.14.4 <a href="00Index.html">Index</a>]</div> </body></html>