繁体   English   中英

在r中添加前导0

[英]Adding leading 0s in r

我有一个大型数据框,其中包含以下字符:

 x <- c("Y188","Y204" ,"Y221","EP121_1" ,"Y233" , "Y248" ,"Y268", "BB2","BB20",  
 "BB32" ,"BB044" ,"BB056" , "Y234" , "Y249" ,"Y271" ,"BB3", "BB21", "BB33",
 "BB045","BB057" ,"Y236", "Y250", "Y272" , "BB4", "BB22" )

如您所见,某些标签(如BB20)只有两个整数。 我希望整个字符列表至少有3个这样的整数(如果有帮助,问题只出现在BB标签中):

Y188, Y204, Y221, EP121_1, Y233, Y248, Y268, BB002, BB020, BB032, BB044,
BB056, Y234, Y249, Y271, BB003, BB021, BB033, BB045, BB057, Y236, Y250,
Y272, BB004, BB022 

我查看了sprintfFormatC函数,但仍然没有运气。

嵌套gsub调用的有力方法:

gsub("(.*[A-Z])(\\d{1}$)", "\\100\\2",
     gsub("(.*[A-Z])(\\d{2}$)", "\\10\\2", x))
# [1] "Y188"    "Y204"    "Y221"    "EP121_1" "Y233"    "Y248"    "Y268"    "BB002"   "BB020"  
# [10] "BB032"   "BB044"   "BB056"   "Y234"    "Y249"    "Y271"    "BB003"   "BB021"   "BB033"  
# [19] "BB045"   "BB057"   "Y236"    "Y250"    "Y272"    "BB004"   "BB022"

肯定有一种更通用的方法可以做到这一点,但是对于这样的本地化任务,两个简单的sub就足够了:为两位数字添加一个尾随零,为一位数字添加两个尾随零。

x <- sub("^BB(\\d{1})$","BB00\\1",x)
x <- sub("^BB(\\d{2})$","BB0\\1",x)

这有效,但会有边缘情况

# indicator for numeric of length less than three
num <- gsub("[^0-9]", "", x)
id <- nchar(num) < 3

# overwrite relevant values with the reformatted ones
x[id] <- paste0(gsub("[0-9]", "", x)[id],
                     formatC(as.numeric(num[id]), width = 3, flag = "0"))

 [1] "Y188"    "Y204"    "Y221"    "EP121_1" "Y233"    "Y248"    "Y268"    "BB002"   "BB020"   "BB032"  
[11] "BB044"   "BB056"   "Y234"    "Y249"    "Y271"    "BB003"   "BB021"   "BB033"   "BB045"   "BB057"  
[21] "Y236"    "Y250"    "Y272"    "BB004"   "BB022"

可以使用sprintfgsub函数完成。此步骤将提取数值并更改其格式。

num = sprintf(“%03d”,as.numeric(gsub(“[^ [:digit:]]”,“”,x)))

下一步是粘贴更改格式的数字

x =粘贴(gsub(“[^ [:alpha:]]”,“”,x),num,sep =“”)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM