I'm sure there is a way to make this more compact, but this is probably the thing you thought about in your second, non-regex idea:
k <- factor(k)
> k1 <- as.integer(k) %% 7
> k1[k1 == 0] <- 7
> LETTERS[k1]
[1] "A" "B" "C" "D" "E" "F" "G" "A" "B" "C" "D" "E" "F" "G" "A" "B" "C" "D" "E" "F" "G" "A"
[23] "B" "C" "D" "E" "F" "G"
There is probably a smart way around the problem with number 0, but at the moment I don't feel terribly smart.
Edit
Good suggestions from the comments. First, for processing form 0, modular arithmetic:
k1 <- ((as.integer(k)-1) %%7) + 1
and in combination with matchit turns into a single line:
k1 <- LETTERS[((match(k, LETTERS)-1) %% 7) + 1]
joran source
share