R:从字符串的开头删除前导零
R: Remove leading zeroes from the beginning of a character string
我首先提到了 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)
})
但是,它只是 returns 列表组件中有前导零的那些元素,而不是没有和也没有 0 Undefined
的其余元素。
有人可以帮忙吗?
我们遍历 list
(lapply(x, ..)
),使用 sub
替换 list
元素中的前导零。我们匹配字符串开头的多个零之一 (^0+
),后跟由正则表达式前瞻 ((?=[1-9])
) 指定的数字 1-9,并将其替换为 ''
.
lapply(x, function(y) sub('^0+(?=[1-9])', '', y, perl=TRUE))
或者正如@hwnd 在评论中提到的那样,我们可以使用捕获组,即代替 lookahead
。
lapply(x, function(y) sub('^0+([1-9])', '\1', y))
或者不使用匿名函数,我们可以指定sub
的pattern
和replacement
参数
lapply(x, sub, pattern='^0+([1-9])', replacement='\1')
我首先提到了 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)
})
但是,它只是 returns 列表组件中有前导零的那些元素,而不是没有和也没有 0 Undefined
的其余元素。
有人可以帮忙吗?
我们遍历 list
(lapply(x, ..)
),使用 sub
替换 list
元素中的前导零。我们匹配字符串开头的多个零之一 (^0+
),后跟由正则表达式前瞻 ((?=[1-9])
) 指定的数字 1-9,并将其替换为 ''
.
lapply(x, function(y) sub('^0+(?=[1-9])', '', y, perl=TRUE))
或者正如@hwnd 在评论中提到的那样,我们可以使用捕获组,即代替 lookahead
。
lapply(x, function(y) sub('^0+([1-9])', '\1', y))
或者不使用匿名函数,我们可以指定sub
pattern
和replacement
参数
lapply(x, sub, pattern='^0+([1-9])', replacement='\1')