【问题标题】:R: Remove leading zeroes from the beginning of a character stringR:从字符串的开头删除前导零
【发布时间】:2015-12-25 01:35:42
【问题描述】:

我首先提到了this question,但答案对我没有帮助。

我有一个列表,其中每个组件都包含以数字开头的元素,然后是单词(字符)。元素开头的一些数字有一个或多个前导零。这是列表的一小部分:

x <- list(el1 = c("0010 First",
                  "0200 Second",
                  "0300 Third",
                  "4000 Fourth",
                  "0 Undefined",
                  "60838 Random",
                  "903200 Haphazard"),
          el2 = c("0100 Hundredth",
                  "0200 Two hundredth",
                  "0300 Three hundredth",
                  "0040 Fortieth",
                  "0 Undefined",
                  "949848 Random",
                  "202626 Haphazard"),
          el3 = c("0010 First",
                  "0200 Second",
                  "0300 Third",
                  "0100 Hundredth",
                  "0200 Two hundredth",
                  "0300 Three hundredth",
                  "0 Undefined",
                  "60838 Random",
                  "20200 Haphazard"))

我想要实现的是删除可用的前导零,并且在0 Undefined 的开头仍然有单个零以及所有​​其他不以前导零开头的元素。也就是说,列表如下:

x <- list(el1 = c("10 First",
                  "200 Second",
                  "300 Third",
                  "4000 Fourth",
                  "0 Undefined",
                  "60838 Random",
                  "903200 Haphazard"),
          el2 = c("100 Hundredth",
                  "200 Two hundredth",
                  "300 Three hundredth",
                  "40 Fortieth",
                  "0 Undefined",
                  "949848 Random",
                  "202626 Haphazard"),
          el3 = c("10 First",
                  "200 Second",
                  "300 Third",
                  "100 Hundredth",
                  "200 Two hundredth",
                  "300 Three hundredth",
                  "0 Undefined",
                  "60838 Random",
                  "20200 Haphazard"))

我已经走了几个小时没有成功。我能做的最好的就是:

lapply(x, function(i) {
  ifelse(grep(pattern = "^0+[1-9]", x = i),
         gsub(pattern = "^0+", replacement = "", x = i), i)
})

但是,它只返回列表组件中存在前导零的元素,而不返回没有0 Undefined 和没有0 Undefined 的其余元素。

有人可以帮忙吗?

【问题讨论】:

    标签: regex r list character leading-zero


    【解决方案1】:

    我们循环遍历list (lapply(x, ..)),使用sub 替换list 元素中的前导零。我们匹配字符串开头的多个零之一 (^0+),后跟正则表达式前瞻 ((?=[1-9])) 指定的数字 1-9,并将其替换为 ''

    lapply(x, function(y) sub('^0+(?=[1-9])', '', y, perl=TRUE))
    

    或者正如 cmets 中提到的@hwnd,我们可以使用捕获组,即代替lookahead

    lapply(x, function(y) sub('^0+([1-9])', '\\1', y))
    

    或者不使用匿名函数,我们可以指定subpatternreplacement参数

    lapply(x, sub, pattern='^0+([1-9])', replacement='\\1')
    

    【讨论】:

    • 您可以省略perl=TRUE 参数,实际上不需要。 lapply(x, function(y) sub('^0+([1-9])', '\\1', y))
    • @panman 我正在使用匿名函数,就像你使用的 function(i)
    • @akrun:哦,我一定很累了,我问了一个多么愚蠢的问题……当然……抱歉打扰了。
    • @panman 你不需要匿名电话lapply(x, sub, pattern='^0+([1-9])', replacement='\\1')
    猜你喜欢
    • 2011-04-09
    • 1970-01-01
    • 2014-08-09
    • 1970-01-01
    • 2014-06-25
    • 2019-10-20
    • 2012-02-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多