View source: R/change_values.R
change_values | R Documentation |
Replace specific values in a vector with new values.
change_values(x, ..., .default = NULL)
x |
A vector in which values will be replaced. |
... |
An ellipsis of pairs of values to be replaced. Each pair consists of the original value and the corresponding replacement value. |
.default |
The default value to be used for elements not specified in the replacement pairs. If not provided, elements not specified will remain unchanged. |
A modified vector with replaced values.
## Retains NAs
change_values(c(1, 2, 3, NA), 2 ~ "two", 3 ~ "three")
## Set a specific default value
change_values(c(1, 2, 3), 2 ~ "two", .default = "default")
## No error message when no value to convert is found
change_values(c(1, 2, 3), 4 ~ "four")
## Recode NAs and implicit conversion from numeric to character
change_values(c(NA, 1, NA, 2), NA ~ "This is a missing value")
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.