Description Usage Arguments Value Note See Also Examples

Recodes (or "renumbers") the categories of variables into new
category values, beginning with the lowest value specified by `lowest`

.
Useful when recoding dummy variables with 1/2 values to 0/1 values, or
recoding scales from 1-4 to 0-3 etc.
`recode_to_if()`

is a scoped variant of `recode_to()`

, where
recoding will be applied only to those variables that match the
logical condition of `predicate`

.

1 2 3 4 5 |

`x` |
A vector or data frame. |

`...` |
Optional, unquoted names of variables that should be selected for
further processing. Required, if |

`lowest` |
Indicating the lowest category value for recoding. Default is 0, so the new variable starts with value 0. |

`highest` |
If specified and greater than |

`append` |
Logical, if |

`suffix` |
String value, will be appended to variable (column) names of
recoded variables ( `rec()` ) will be suffixed with`"_r"` recoded variables ( `recode_to()` ) will be suffixed with`"_r0"` dichotomized variables ( `dicho()` ) will be suffixed with`"_d"` grouped variables ( `split_var()` ) will be suffixed with`"_g"` grouped variables ( `group_var()` ) will be suffixed with`"_gr"` standardized variables ( `std()` ) will be suffixed with`"_z"` centered variables ( `center()` ) will be suffixed with`"_c"` de-meaned variables ( `de_mean()` ) will be suffixed with`"_dm"` grouped-meaned variables ( `de_mean()` ) will be suffixed with`"_gm"`
If |

`predicate` |
A predicate function to be applied to the columns. The
variables for which |

`x`

with recoded category values, where `lowest`

indicates
the lowest value; If `x`

is a data frame, for `append = TRUE`

,
`x`

including the recoded variables as new columns is returned; if
`append = FALSE`

, only the recoded variables will be returned. If
`append = TRUE`

and `suffix = ""`

, recoded variables will replace
(overwrite) existing variables.

Value and variable label attributes are preserved.

`rec`

for general recoding of variables and `set_na`

for setting `NA`

values.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | ```
# recode 1-4 to 0-3
dummy <- sample(1:4, 10, replace = TRUE)
recode_to(dummy)
# recode 3-6 to 0-3
# note that numeric type is returned
dummy <- as.factor(3:6)
recode_to(dummy)
# lowest value starting with 1
dummy <- sample(11:15, 10, replace = TRUE)
recode_to(dummy, lowest = 1)
# lowest value starting with 1, highest with 3
# all others set to NA
dummy <- sample(11:15, 10, replace = TRUE)
recode_to(dummy, lowest = 1, highest = 3)
# recode multiple variables at once
data(efc)
recode_to(efc, c82cop1, c83cop2, c84cop3, append = FALSE)
library(dplyr)
efc %>%
select(c82cop1, c83cop2, c84cop3) %>%
mutate(
c82new = recode_to(c83cop2, lowest = 5),
c83new = recode_to(c84cop3, lowest = 3)
) %>%
head()
``` |

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.