R代码生成前缀为

时间:2016-10-09 19:56:09

标签: r

我有一张名为" pipel"包含超过10,000行。我想添加一个ID列来为每一行分配一个唯一的ID。唯一ID必须为30位数字,并以" AKM_CC_Test _"开头。我使用下面的代码作为起点但不确定如何格式化它以添加前缀并使其长达30位。

id <- rownames(pipel)
pipel <- cbind(id=id, pipel)

例如,第一行ID需要看起来像这样AKM_CC_Test_000000000000000001

2 个答案:

答案 0 :(得分:7)

您可以使用sprintf()。这将创建一个以"AKM_CC_Test_"开头的30个字符的字符串,并以1:nrow(pipel)的序列结尾,并带有前导零。

x <- "AKM_CC_Test_"
sprintf("%s%0*d", x, 30 - nchar(x), 1:nrow(pipel))
  • %sx插入字符串
  • %0*d1:nrow(pipel)之后添加* x前导零。 *用于将30 - nchar(x)插入格式中(我以编程方式执行;如果需要,可以在其中插入18个)

关于简单长度5(1:5)向量的示例将是

x <- "AKM_CC_Test_"
sprintf("%s%0*d", x, 30 - nchar(x), 1:5)
# [1] "AKM_CC_Test_000000000000000001" "AKM_CC_Test_000000000000000002"
# [3] "AKM_CC_Test_000000000000000003" "AKM_CC_Test_000000000000000004"
# [5] "AKM_CC_Test_000000000000000005"

答案 1 :(得分:-2)

您可以使用:seq作为序列,并可以使用pastepaste0添加前导文字。问题的核心在于带有前导0的数字填充。

您的选择是:

    来自stri_pad
  1. stringi(更直观)
  2. 来自str_pad
  3. stringr(更直观)
  4. sprintf(不需要包裹)
  5. formatC(如果您熟悉C&#39; s printf
  6. 请注意,某些情况(尽管不是特定的情况)需要禁用序列中数字的科学记数法。可以使用options中的with_optionsdevtools来完成此操作。

    请参阅this popular post了解每个示例。

    使用formatC

    uid <- paste0("AKM_CC_Test_", formatC(1:10000, width = 18, format = "d", flag = "0"))
    head(uid)
    
    [1] "AKM_CC_Test_000000000000000001" "AKM_CC_Test_000000000000000002" "AKM_CC_Test_000000000000000003" "AKM_CC_Test_000000000000000004"
    [5] "AKM_CC_Test_000000000000000005" "AKM_CC_Test_000000000000000006"
    

    使用stringr包:

    uid <- paste0("AKM_CC_Test_", str_pad(1:10000, 18, pad = "0")) # remember to load stringr
    head(uid)
    
    [1] "AKM_CC_Test_000000000000000001" "AKM_CC_Test_000000000000000002" "AKM_CC_Test_000000000000000003" "AKM_CC_Test_000000000000000004"
    [5] "AKM_CC_Test_000000000000000005" "AKM_CC_Test_000000000000000006"
    

    使用sprintf

    head(sprintf("%s%0*d", "AKM_CC_Test_", 18,  1:10000))
    
    [1] "AKM_CC_Test_000000000000000001" "AKM_CC_Test_000000000000000002" "AKM_CC_Test_000000000000000003" "AKM_CC_Test_000000000000000004"
    [5] "AKM_CC_Test_000000000000000005" "AKM_CC_Test_000000000000000006"
    

    使用包stri_pad中的stringi

    uid <- paste0("AKM_CC_Test_", stri_pad(1:10000, 18, pad = "0")) # remember to load stringi
    head(uid)
    
    [1] "AKM_CC_Test_000000000000000001" "AKM_CC_Test_000000000000000002" "AKM_CC_Test_000000000000000003" "AKM_CC_Test_000000000000000004"
    [5] "AKM_CC_Test_000000000000000005" "AKM_CC_Test_000000000000000006"