Replace Multiple Values in a Column for a Single One

Replacing multiple values in 1 column to a single value in R

With base R, we can create search terms that we want to replace (i.e., "eat|drinks|shop|eat and shop"). The | means "or", so we will look for eat or drinks or ... etc. If we find those terms, then we will replace them with Companionship.

data$activity <- gsub(paste(leisure, collapse = "|"), "Companionship", data$activity)

activity
1 Companionship
2 sing
3 dance
4 sing
5 Companionship
6 Companionship
7 Companionship

Or with tidyverse:

library(tidyverse)

data %>%
mutate(activity = str_replace_all(activity, paste(leisure, collapse = "|", "$", sep = ""), "Companionship"))

Or with your approach, you just need to use %in% rather than ==, as you want to match to any value in leisure.

data$activity[data$activity %in% leisure] <- "Companionship"

replace multiple values in a column for a single one

You can use function recode() from library car to change values also for the factors.

library(car)
a$T<-recode(a$T,"c('E','S','T')='AB'")

If you need to replace different values with different other values then all statements can be written in one function call.

recode(a$T,"c('E','S','T')='AB';c('F','G','H')='CD'")

How to replace multiple values in a single column with SQL?

Use case:

select case updated_by
when '5eaf5d368141560012161636' then 'A'
when '5e79d03e9abae00012ffdbb3' then 'B'
when '5e7b501e9abae00012ffdbd6' then 'C'
when '5e7b5b199abae00012ffdbde' then 'D'
when '5e7c817c9ca5540012ea6cba' then 'E'
end as updated_by
from my_table

Replace multiple characters with multiple values in multiple columns? R

You can use dplyr::recode

df <- data.frame(name = rep(letters[1:3], each = 3), foo=rep(1:9),var1 = letters[1:3], var2 = rep(3:5, each = 3))

library(dplyr, warn.conflicts = FALSE)

df %>%
mutate(across(c(name, var1), ~ recode(., a = 1, b = 2, c = 3)))
#> name foo var1 var2
#> 1 1 1 1 3
#> 2 1 2 2 3
#> 3 1 3 3 3
#> 4 2 4 1 4
#> 5 2 5 2 4
#> 6 2 6 3 4
#> 7 3 7 1 5
#> 8 3 8 2 5
#> 9 3 9 3 5

Created on 2021-10-19 by the reprex package (v2.0.1)

Across will apply the function defined by ~ recode(., a = 1, b = 2, c = 3) to both name and var1.

Using ~ and . is another way to define a function in across. This function is equivalent to the one defined by function(x) recode(x, a = 1, b = 2, c = 3), and you could use that code in across instead of the ~ form and it would give the same result. The only name I know for this is what it's called in ?across, which is "purrr-style lambda function", because the purrr package was the first to use formulas to define functions in this way.

If you want to see the actual function created by the formula, you can look at rlang::as_function(~ recode(., a = 1, b = 2, c = 3)), although it's a little more complex than the one above to support the use of ..1, ..2 and ..3 which are not used here.

Now that R supports the easier way of defining functions below, this purrr-style function is maybe no longer useful, it's just an old habit to write it that way.

df <- data.frame(name = rep(letters[1:3], each = 3), foo=rep(1:9),var1 = letters[1:3], var2 = rep(3:5, each = 3))

library(dplyr, warn.conflicts = FALSE)

df %>%
mutate(across(c(name, var1), \(x) recode(x, a = 1, b = 2, c = 3)))
#> name foo var1 var2
#> 1 1 1 1 3
#> 2 1 2 2 3
#> 3 1 3 3 3
#> 4 2 4 1 4
#> 5 2 5 2 4
#> 6 2 6 3 4
#> 7 3 7 1 5
#> 8 3 8 2 5
#> 9 3 9 3 5

Created on 2021-10-19 by the reprex package (v2.0.1)

dplyr: Replace multiple values based on condition in a selection of columns

A dplyr solution:

library(dplyr)
dt %>%
mutate(across(3:5, ~ ifelse(measure == "led", stringr::str_replace_all(
as.character(.),
c("2" = "X", "3" = "Y")
), .)))

Result:

   measure site space qty qty.exit cf
1: led 4 1 4 6 3
2: exit 4 2 1 4 6
3: cfl 1 4 6 2 3
4: linear 3 4 1 3 5
5: cfl 5 1 6 1 6
6: exit 4 3 2 6 4
7: exit 5 1 4 2 5
8: exit 1 4 3 6 4
9: linear 3 1 5 4 1
10: led 4 1 1 1 1
11: exit 5 4 3 5 2
12: cfl 4 2 4 5 5
13: led 4 X Y Y 4
...


Related Topics



Leave a reply



Submit