Replace multiple strings in a column of a data frame
You can do the following to add as many pattern-replacement pairs as you want in one line.
library(stringr)
vec <- c("Absent", "Absent", "Present", "Present", "XX", "YY", "ZZ")
str_replace_all(vec, c("Absent" = "A", "Present" = "P"))
# [1] "A" "A" "P" "P" "XX" "YY" "ZZ"
Replace multiple strings in one gsub() or chartr() statement in R?
You can use gsubfn
library(gsubfn)
gsubfn(".", list("'" = "", " " = "_"), x)
# [1] "ab_c"
Similarly, we can also use mgsub
which allows multiple replacement with multiple pattern to search
mgsub::mgsub(x, c("'", " "), c("", "_"))
#[1] "ab_c"
Replace multiple strings in one column with other values
Alternatively to dpyr
, you could try using ifelse
within ifelse
ifelse(x == "a","Apple",ifelse(x == "c", "cat", x))
[1] "cat" "cat" "Apple" "cat" "b" "b" "cat" "Apple" "b" "cat"
How to replace multiple strings with the same in R
sub("blue|red", "colour", vec)
use "|" (which means the logical OR
operator) between the words you want to substitute.
Use sub
to change only the first occurence and gsub
to change multiple occurences within the same string.
Type ?gsub
into R console for more information.
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)
Replace multiple similar values in a column in R
Perhaps adding 3 and pasting " years old" will satisfy your needs?
data$txtAge <- paste(data$Age, "years old")
There is no need for an iterative command. R's functions often iterate automagically. In this case the paste
command is designed to return character results of the same length as the longest input argument but it "recycles" (repeats) the shorter argument. You would get a column of the same length as there were rows in the data
object.
Related Topics
Accessing Parent Namespace Inside a Shiny Module
Dplyr::Select() with Some Variables That May Not Exist in the Data Frame
Create Combinations of a Binary Vector
How to Configure R-3.1.2 with --Enable-R-Shlib
How to Minimize Size of Object of Class "Lm" Without Compromising It Being Passed to Predict()
Connect R and Vertica Using Rodbc
Rselenium, Chrome, How to Set Download Directory, File Download Error
In R, Check If String Appears in Row of Dataframe (In Any Column)
How to Make a Barplot with R from a Table
Remove Unused Categorical Values Boxplot - R
R Predict Function Returning Too Many Values
Replacing White Space with One Single Backslash
Flattening a Delimited Composite Column