Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replace / translate characters in a string

Tags:

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?

like image 794
jrara Avatar asked Aug 05 '11 09:08

jrara


2 Answers

You want chartr:

df$var1 <- chartr("abcdef", "hijklm", df$var1)
df
#        var1
# 1 hhiijklmg
# 2 hhiijklmg
like image 195
Marek Avatar answered Oct 03 '22 13:10

Marek


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
like image 21
Jean-Robert Avatar answered Oct 03 '22 15:10

Jean-Robert