'Generating strings combining letter and number sequences in R

How can i generate this vector in R:

x <- c("R11", "R12", "R13", "R21", "R22", "R23", "R31",....) until R7xx for a set of six letters (R, S, D, A, B, X)?

and so on? Without actually typing it.

r


Solution 1:[1]

x <- 10
i<- 0
abc <- array()
xyz <- array()
words <- c('R','S','D','A','B','X')


for(j in 1:6) {
for(i in 1:7) {
abc <- paste(words[j], (x*i + 1):(x*i + 3), sep = "")
xyz <- c(xyz, abc)
}
}

xyz
[1] NA    "R11" "R12" "R13" "R21" "R22" "R23" "R31" "R32" "R33" "R41" "R42" "R43" "R51" "R52" "R53"
[17] "R61" "R62" "R63" "R71" "R72" "R73" "S11" "S12" "S13" "S21" "S22" "S23" "S31" "S32" "S33" "S41"
[33] "S42" "S43" "S51" "S52" "S53" "S61" "S62" "S63" "S71" "S72" "S73" "D11" "D12" "D13" "D21" "D22"
[49] "D23" "D31" "D32" "D33" "D41" "D42" "D43" "D51" "D52" "D53" "D61" "D62" "D63" "D71" "D72" "D73"
[65] "A11" "A12" "A13" "A21" "A22" "A23" "A31" "A32" "A33" "A41" "A42" "A43" "A51" "A52" "A53" "A61"
[81]"A62" "A63" "A71" "A72" "A73" "B11" "B12" "B13" "B21" "B22" "B23" "B31" "B32" "B33" "B41" "B42"
[97] "B43" "B51" "B52" "B53" "B61" "B62" "B63" "B71" "B72" "B73" "X11" "X12" "X13" "X21" "X22" "X23"
[113] "X31" "X32" "X33" "X41" "X42" "X43" "X51" "X52" "X53" "X61" "X62" "X63" "X71" "X72" "X73"

NA gets generated as first value. To remove that NA you can use,

xyz <- xyz[-1]

Solution 2:[2]

expand.grid begins with the first argument to circle. In case this should be the opposite way, the arguments to expand.grid need to be given reverse and its result needs to be reversed again.

do.call(paste0, rev(expand.grid(1:3, 1:7, c("R", "S", "D", "A", "B", "X"))))
#  [1] "R11" "R12" "R13" "R21" "R22" "R23" "R31" "R32" "R33" "R41" "R42" "R43"
# [13] "R51" "R52" "R53" "R61" "R62" "R63" "R71" "R72" "R73" "S11" "S12" "S13"
# [25] "S21" "S22" "S23" "S31" "S32" "S33" "S41" "S42" "S43" "S51" "S52" "S53"
# [37] "S61" "S62" "S63" "S71" "S72" "S73" "D11" "D12" "D13" "D21" "D22" "D23"
# [49] "D31" "D32" "D33" "D41" "D42" "D43" "D51" "D52" "D53" "D61" "D62" "D63"
# [61] "D71" "D72" "D73" "A11" "A12" "A13" "A21" "A22" "A23" "A31" "A32" "A33"
# [73] "A41" "A42" "A43" "A51" "A52" "A53" "A61" "A62" "A63" "A71" "A72" "A73"
# [85] "B11" "B12" "B13" "B21" "B22" "B23" "B31" "B32" "B33" "B41" "B42" "B43"
# [97] "B51" "B52" "B53" "B61" "B62" "B63" "B71" "B72" "B73" "X11" "X12" "X13"
#[109] "X21" "X22" "X23" "X31" "X32" "X33" "X41" "X42" "X43" "X51" "X52" "X53"
#[121] "X61" "X62" "X63" "X71" "X72" "X73"

In case the output should be a factor expand.grid with interaction could be used.

interaction(rev(expand.grid(1:3, 1:7, c("R", "S", "D", "A", "B", "X"))), sep="")
#  [1] R11 R12 R13 R21 R22 R23 R31 R32 R33 R41 R42 R43 R51 R52 R53 R61 R62 R63
# [19] R71 R72 R73 S11 S12 S13 S21 S22 S23 S31 S32 S33 S41 S42 S43 S51 S52 S53
# [37] S61 S62 S63 S71 S72 S73 D11 D12 D13 D21 D22 D23 D31 D32 D33 D41 D42 D43
# [55] D51 D52 D53 D61 D62 D63 D71 D72 D73 A11 A12 A13 A21 A22 A23 A31 A32 A33
# [73] A41 A42 A43 A51 A52 A53 A61 A62 A63 A71 A72 A73 B11 B12 B13 B21 B22 B23
# [91] B31 B32 B33 B41 B42 B43 B51 B52 B53 B61 B62 B63 B71 B72 B73 X11 X12 X13
#[109] X21 X22 X23 X31 X32 X33 X41 X42 X43 X51 X52 X53 X61 X62 X63 X71 X72 X73
#126 Levels: R11 S11 D11 A11 B11 X11 R21 S21 D21 A21 B21 X21 R31 S31 D31 ... X73

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 GKi