Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have the following vector:

x <- c(11, 12, 21, 22)

And I want to convert it to the corresponding letters, i.e., I want to get this result:

AA AB BA BB

How do I make this? I bet there's a simple answer and that it goes through using the reserved LETTERS vector, but I can't figure out a solution. This is the best I've managed to come up with so far (you might want to take the kids out of the room):

> paste0(gsub(1, LETTERS[1], substr(x, 1, 1)),
         gsub(2, LETTERS[2], substr(x, 1, 1)))
[1] "A1" "A1" "2B" "2B"
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
1.0k views
Welcome To Ask or Share your Answers For Others

1 Answer

Since this just involves one-to-one character substitution, it might be simplest to just use chartr()

chartr("123456789", "ABCDEFGHI", x)
# [1] "AA" "AB" "BA" "BB"

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...