0

This question relates to this previously asked question about creating a sequential list of letters. I am currently trying to created a list of variables that is both numerically and alphabetically ascending. Essentially i want a list of something like this:

"var_1A" "var_1B" var_"1C" "var_1D" "var_2A" "var_2B" var_"2C" "var_2D"

I can easily create a list of variable ascending numerically

paste("var_", 1:2, "A", sep="")

or alphabetically

paste("var_1", letters[1:4], sep="")

but combining the two yields:

paste("var_", 1:2, letters[1:4], sep="")
[1] "var_1a" "var_2b" "var_1c" "var_2d"

How can I get the desired outcome above?

Pål Bjartan
  • 793
  • 1
  • 6
  • 18

2 Answers2

2
foo <- expand.grid(1:2, LETTERS[1:4])
paste0("var_", foo[,1], foo[,2])

[1] "var_1A" "var_2A" "var_1B" "var_2B" "var_1C" "var_2C" "var_1D" "var_2D"
Georgery
  • 7,643
  • 1
  • 19
  • 52
1

You could use outer

c(t(outer(paste0("var_", 1:2), LETTERS[1:4], paste0)))
#[1] "var_1A" "var_1B" "var_1C" "var_1D" "var_2A" "var_2B" "var_2C" "var_2D"

Or another option with paste0 and rep

paste0(rep(paste0("var_", 1:2), each = 4), LETTERS[1:4])
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
  • Thanks! This is exactly what I was looking for. The second option seems somewhat simpler, though. Is there a reason to choose one solution over the other? – Pål Bjartan Feb 11 '20 at 09:56