Description Usage Arguments Value Examples

Compared to the base `ifelse()`

, this function is more strict.
It checks that `true`

and `false`

are the same type. This
strictness makes the output type more predictable, and makes it somewhat
faster.

1 |

`condition` |
Logical vector |

`true, false` |
Values to use for |

`missing` |
If not |

Where `condition`

is `TRUE`

, the matching value from
`true`

, where it's `FALSE`

, the matching value from `false`

,
otherwise `NA`

.

1 2 3 4 5 6 7 8 9 | ```
x <- c(-5:5, NA)
if_else(x < 0, NA_integer_, x)
if_else(x < 0, "negative", "positive", "missing")
# Unlike ifelse, if_else preserves types
x <- factor(sample(letters[1:5], 10, replace = TRUE))
ifelse(x %in% c("a", "b", "c"), x, factor(NA))
if_else(x %in% c("a", "b", "c"), x, factor(NA))
# Attributes are taken from the `true` vector,
``` |

```
Attaching package: 'dplyr'
The following objects are masked from 'package:stats':
filter, lag
The following objects are masked from 'package:base':
intersect, setdiff, setequal, union
[1] NA NA NA NA NA 0 1 2 3 4 5 NA
[1] "negative" "negative" "negative" "negative" "negative" "positive"
[7] "positive" "positive" "positive" "positive" "positive" "missing"
[1] NA NA 1 NA 2 NA NA NA NA NA
[1] <NA> <NA> a <NA> c <NA> <NA> <NA> <NA> <NA>
Levels: a c d e
```

Embedding an R snippet on your website

Add the following code to your website.

For more information on customizing the embed code, read Embedding Snippets.