Hmisc (version 2.0-0)

translate: Translate Vector or Matrix of Text Strings

Description

Uses the UNIX tr command to translate any character in old in text to the corresponding character in new. If multichar=T or old and new have more than one element, or each have one element but they have different numbers of characters, uses the UNIX sed command to translate the series of characters in old to the series in new when these characters occur in text. If old or new contain a backslash, you sometimes have to quadruple it to make the UNIX command work. If they contain a forward slash, preceed it by two backslashes. The Microsoft Windows version of translate invokes the sedit() function and does not allow multichar=FALSE, i.e., it does not support the UNIX tr function. The R version of translate invokes the builtin chartr function if multichar=FALSE.

Usage

translate(text, old, new, multichar=!under.unix)

Arguments

text
scalar, vector, or matrix of character strings to translate.
old
vector old characters
new
corresponding vector of new characters
multichar
See above.

Value

  • an object like text but with characters translated

Details

At present, multichar=FALSE, which requires the UNIX tr program, is not implemented under MS Windows.

See Also

unix, grep

Examples

Run this code
translate(c("ABC","DEF"),"ABCDEFG", "abcdefg")
translate("23.12","[.]","\\cdot ") # change . to \\cdot
translate(c("dog","cat","tiger"),c("dog","cat"),c("DOG","CAT"))
# S-Plus gives  [1] "DOG"   "CAT"   "tiger" - check discrepency
translate(c("dog","cat2","snake"),c("dog","cat"),"animal")
# S-Plus gives  [1] "animal"  "animal2" "snake"

Run the code above in your browser using DataCamp Workspace