I stuck on a minor problem and I haven't found the right search terms for it.
I have letters from "A" - "N" and want to replace these one greater than "G" with "A"-"G" according to their position in the alphabet. using gsub
for that seems cumbersome. Or are there any regex that can do it smarter?
k <- rep(LETTERS[1:14],2)
gsub(pattern="H", replace="A", x=k)
gsub(pattern="I", replace="B", x=k)
gsub(pattern="J", replace="C", x=k)
gsub(pattern="K", replace="D", x=k)
# etc.
Isn't there some way I can convert the the characters to integer and then simply calculate within the integer values and afterwards casting back? Or is there any inverse of LETTERS?
as.numeric()
and as.integer()
returns NA
.
This translates H-N to A-G:
chartr("HIJKLMN", "ABCDEFG", k)
My first thought whenever I see problems like this is match
:
AG <- LETTERS[1:7]
HN <- LETTERS[8:14]
k <- rep(LETTERS[1:14],2)
n <- AG[match(k, HN)]
ifelse(is.na(n), k, n)
# [1] "A" "B" "C" "D" "E" "F" "G" "A" "B" "C" "D" "E" "F" "G" "A" "B" "C" "D" "E"
#[20] "F" "G" "A" "B" "C" "D" "E" "F" "G"
I'd construct an inverse LETTERS
function the same way:
invLETTERS <- function(x) match(x, LETTERS[1:26])
invLETTERS(k)
# [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 1 2 3 4 5 6 7 8 9 10 11
#[26] 12 13 14
Here's a clean and straightforward solution:
k <- rep(LETTERS[1:14],2)
# (1) Create a lookup vector whose elements can be indexed into
# by their names and will return their associated values
subs <- setNames(rep(LETTERS[1:7], 2), LETTERS[1:14])
subs
# A B C D E F G H I J K L M N
# "A" "B" "C" "D" "E" "F" "G" "A" "B" "C" "D" "E" "F" "G"
# (2) Use it.
unname(subs[k])
# [1] "A" "B" "C" "D" "E" "F" "G" "A" "B" "C" "D" "E" "F" "G"
# [15] "A" "B" "C" "D" "E" "F" "G" "A" "B" "C" "D" "E" "F" "G"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With