制作一个重复的字母数字列表

时间:2011-09-01 21:05:40

标签: r repeat

我想制作一个这样的列表:

"A001:A048", "B001:B048", ..., "Z001:Z048", "AA001:AA048", "BB001:BB048", ...

我看了this thread,但无法弄清楚如何根据我的重复字母进行调整。

感谢您的帮助。

2 个答案:

答案 0 :(得分:4)

c( sprintf("%s001:%s048", LETTERS,LETTERS),
   sprintf("%s%s001:%s%s048", LETTERS,LETTERS,LETTERS, LETTERS) )

以下是使用sprintf使用“索引替换”(我的术语)的示例:

 outer(LETTERS, 1:26, FUN=sprintf, fmt="%1$s%1$s%2$03d:%1$s%1$s%2$03d")
 #     [,1]          [,2]          [,3]          [,4]          [,5]         
 [1,] "AA001:AA001" "AA002:AA002" "AA003:AA003" "AA004:AA004" "AA005:AA005"
 [2,] "BB001:BB001" "BB002:BB002" "BB003:BB003" "BB004:BB004" "BB005:BB005"
 [3,] "CC001:CC001" "CC002:CC002" "CC003:CC003" "CC004:CC004" "CC005:CC005"
 [4,] "DD001:DD001" "DD002:DD002" "DD003:DD003" "DD004:DD004" "DD005:DD005"
 [5,] "EE001:EE001" "EE002:EE002" "EE003:EE003" "EE004:EE004" "EE005:EE005"
 snipped a couple of pages of output

又一次用A:AF 1:48组合拍摄:

outer( c(LETTERS,paste("A",LETTERS[1:6],sep="")), 
       1:48, 
       FUN=sprintf, 
       fmt="%1$s%1$s%2$03d")
 #-----------------------------------
 #    [,1]      [,2]      [,3]      [,4]      [,5]      [,6]      [,7]      [,8]     
 [1,] "A001"  "A002"  "A003"  "A004"  "A005"  "A006"  "A007"  "A008"  "A009"  "A010" 
 [2,] "B001"  "B002"  "B003"  "B004"  "B005"  "B006"  "B007"  "B008"  "B009"
   snipped
      [,41]   [,42]   [,43]   [,44]   [,45]   [,46]   [,47]   [,48]  
   snipped
[31,] "AE041" "AE042" "AE043" "AE044" "AE045" "AE046" "AE047" "AE048"
[32,] "AF041" "AF042" "AF043" "AF044" "AF045" "AF046" "AF047" "AF048"

答案 1 :(得分:2)

我认为这就是你想要的,即使你的问题不明确。我使用sprintf因为它使前导零的填充更容易。

prefix <- c(LETTERS,paste("A",LETTERS[1:6],sep=""))
out <- sapply(prefix, function(x) sprintf("%s%03d",x,1:48))
as.vector(out) # if you want a vector instead