替换字符

基本类型 / 字符串

text <- "1-3-2"

# One character for another
plus <- chartr("-", "+", text)
# plus = 1+3+2

# Swap: every 3 becomes 2 and every 2 becomes 3
swapped <- chartr("32", "23", text)
# swapped = 1-2-3

cat("plus =", plus, "\n")
cat("swapped =", swapped, "\n")

# text[3] is a third string, not a character
broken <- text
broken[3] <- "2"
str(broken)