I have a data frame with a character column:
df <- data.frame(var1 = c("aabbcdefg", "aabbcdefg"))
df
# var1
# 1 aabbcdefg
# 2 aabbcdefg
I want to replace several different individual characters, e.g. from "a" to "h", from "b" to "i" and so on. Currently I use several calls to gsub
:
df$var1 <- gsub("a", "h", df$var1)
df$var1 <- gsub("b", "i", df$var1)
df$var1 <- gsub("c", "j", df$var1)
df$var1 <- gsub("d", "k", df$var1)
df$var1 <- gsub("e", "l", df$var1)
df$var1 <- gsub("f", "m", df$var1)
df
# var1
# 1 hhiijklmg
# 2 hhiijklmg
However, I'm sure there are more elegant solutions. Any ideas ho to proceed?
You want chartr
:
df$var1 <- chartr("abcdef", "hijklm", df$var1)
df
# var1
# 1 hhiijklmg
# 2 hhiijklmg
You can create from
and to
vectors:
from <- c('a','b','c','d','e','f')
to <- c('h','i','j','k','l','m')
and then vectorialize the gsub
function:
gsub2 <- function(pattern, replacement, x, ...) {
for(i in 1:length(pattern))
x <- gsub(pattern[i], replacement[i], x, ...)
x
}
Which gives:
> df <- data.frame(var1 = c("aabbcdefg", "aabbcdefg"))
> df$var1 <- gsub2(from, to, df$var1)
> df
var1
1 hhiijklmg
2 hhiijklmg
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With