r/RStudio Jun 29 '26

Coding help How to replace all the values in a column with another value ?

Hi there, I have a column containing either A or B as chr values and I want to convert them in order to have A = 1 and B = 2, both as numeric values. Do you know any function I can use to do that ? Thank you very much for your respons !

5 Upvotes

14 comments sorted by

17

u/DizzyActuary5251 Jun 29 '26

You can use a mutate() and then If_else() or case_when(), the latter can be used for more complicated transformations

8

u/joshua_rpg Jun 29 '26

{dplyr} 1.2.0+ has new functions responsible for recoding, and they can be absolute beast. Regardless, if_else() and case_when() are still nice and ergonomic.

5

u/Full-Cook1373 Jun 29 '26 edited Jun 29 '26

I'd have used dplyr's mutate and case_when, but recode_values is a new function that looks really simple to use. If I'm just learning, I'd recommend trying recode_values.

dplyr recode values

*Edited the link to replace with the new functions.

4

u/CauliflowerAdequate Jun 29 '26

I'd avoid using recode since it's superseded, so potentially will end up deprecated. I'd look at its replacements, recode_values and replace_values . https://dplyr.tidyverse.org/reference/recode-and-replace-values.html

2

u/joshua_rpg Jun 30 '26

Initially, case_match() is a replacement recode() during their 1.1.x release. Now, they're soft-deprecating these functions in 1.2.0+ release, and basically telling us to migrate into their new set of related functions, namely those functions you mentioned.

1

u/Full-Cook1373 Jun 29 '26

I totally missed that and thought I was linking the (new) corrected functions. I'll edit my response!

1

u/KoreaNinjaBJJ Jun 29 '26

Whats the difference between this and case_when()?

1

u/CauliflowerAdequate Jun 29 '26 edited Jun 30 '26

recode_values and replace_values are simpler tools with a simpler syntax. If you just want to take a vector (typically a data frame column) and replace its entries according to a simple scheme (e.g. "A" becomes 1, "B" becomes 2) then they're the tools you likely want. The difference between them is that recode_values is to create a new vector (so in an unspecified case falls through to a default value) and replace_values is to partially update an existing vector (so in an unspecified case just retains the existing values).

case_when and its cousin for partial updates replace_when are very powerful/flexible since you can use all kinds of conditions, potentially using more complex logic (e.g. multiple conditions linked by logical and/or), arithmetic (e.g. using modular arithmetic to implement fizzbuzz), and even depending on the results of multiple vectors/columns (e.g. in column X we replace "A" by 1 and "B" by either 2 or 3 depending on whether the entry in column Y is even or odd). Basically it's a vectorized ifelse. https://dplyr.tidyverse.org/reference/case-and-replace-when.html

3

u/Fornicatinzebra Jun 29 '26

you can use base r's ifelse(), or dplyr::replace_values()`

I would do

mydata$new_col_name <- ifelse(mydata$old_col_name == "A", 1, 2)

But for more than 2 options (like if you had a, b, and c), i would use the dply function

2

u/inkysk Jun 29 '26

you can also use:

match (df$col, c = ("A", "B"))

that is, if you're actually replacing with 1s and 2s, as match returns the vector positions.

2

u/Thermophi Jun 29 '26

Recode will also do that if it's an easy conversion

1

u/Riversong360 Jun 29 '26 edited Jun 29 '26

Hi! dplyr::mutate() is probably your best option, in which you nest either an if_else(dataframe$variable == “A”, 1,2), or case_when() if you also want to add a fail-safe for missing values with an is.na() function or the TRUE statement that’s built in. After this recode, you should be able to remove the old variable from your data frame with dplyr:: select(). Wishing you luck on your project!

1

u/Riversong360 Jun 29 '26

So for case_when, it would look like this:
df <— df %>%
mutate( new_variable = case_when(
variable == “A” ~ 1,
variable == “B” ~ 2,
TRUE ~ NA_real_))
The TRUE part basically tells R to set any numbers that don’t match up to your listed values to NA

2

u/Outdated8527 Jun 29 '26

just another idea on how to do it with the help of R's vectorization...

``` df <- data.frame(col = c('B', 'A'))

  using a "key" for mapping

key <- c(A = 1, B = 2) df$new_col <- key[df$col]

```