0

Say i got an R dataframe A like so:

  a   b
1 nbc dkf
2 lei ao

How do I get a new dataframe B:

  a   b
1 gfr ais 
2 ykl db

If I have two vectors that contain the starting characters and the ending characters like so:

start = c(a,b,c,d,e,f,l,k,i,n,o)
end =   c(d,f,r,a,k,s,y,i,l,g,b)
Mario
  • 561
  • 3
  • 18

1 Answers1

1
start <- paste(start, collapse="")
end <- paste(end, collapse="")

dfB <- df
dfB[] <- lapply(dfB, chartr, old = start, new = end)
dfB

    a   b
1 gfr asi
2 ykl  db

Reproducible data (please provide yourself next time):

df <- data.frame(
  a = c("nbc", "lei"),
  b = c("dfk", "ao")
)
start = c("a", "b", "c", "d", "e", "f", "l", "k", "i", "n", "o")
end =   c("d", "f", "r", "a", "k", "s", "y", "i", "l", "g", "b")
s_baldur
  • 29,441
  • 4
  • 36
  • 69