Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/76.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/431.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 在混合字符串之间添加前导零_R - Fatal编程技术网

R 在混合字符串之间添加前导零

R 在混合字符串之间添加前导零,r,R,我有一个向量 x <- c("MB1", "MB11" ,"MB12" ,"MB13", "B1", "B11", "B12", "B13", "B2") x仅包含“MB”或“B”作为前导字符串,后跟最多两位数字 我知道如何使用stru_pad,所以我想执行以下操作 new.vector < past

我有一个向量

x <- c("MB1",  "MB11" ,"MB12" ,"MB13", "B1",  "B11", "B12", "B13", "B2")
x仅包含“MB”或“B”作为前导字符串,后跟最多两位数字

我知道如何使用stru_pad,所以我想执行以下操作

new.vector < paste0("Any Letter you find in each element of x", str_pad("numerical elements of x", 2, pad="0"))
new.vector
或者任何其他可以实现这一点的方法

谢谢

1)gsub分别删除数字和非数字,然后使用sprintf组合回。没有使用任何软件包

library(tidyverse)

strcapture('([A-Z]+)([0-9]+)', x, 
           proto = list(char = character(), num = numeric())) %>%
  mutate(num = str_pad(num, 2, pad = '0')) %>%
  unite(value, char, num, sep = '') %>%
  pull(value)

#[1] "MB01" "MB11" "MB12" "MB13" "B01"  "B11"  "B12"  "B13"  "B02" 
sprintf("%s%02d", gsub("\\d", "", x), as.numeric(gsub("\\D", "", x)))
## [1] "MB01" "MB11" "MB12" "MB13" "B01"  "B11"  "B12"  "B13"  "B02" 
sub("(\\D)(\\d)$", "\\10\\2", x)
## [1] "MB01" "MB11" "MB12" "MB13" "B01"  "B11"  "B12"  "B13"  "B02" 
digits <- gsub("\\D", "", x)
nondigits <- gsub("\\d", "", x)
sprintf("%s%0*d", nondigits, max(nchar(digits)), as.numeric(digits))
## [1] "MB01" "MB11" "MB12" "MB13" "B01"  "B11"  "B12"  "B13"  "B02" 
2)trimws使用trimws的此变体也仅使用基准R

sprintf("%s%02d", trimws(x,, "\\d"), as.numeric(trimws(x,, "\\D")))
## [1] "MB01" "MB11" "MB12" "MB13" "B01"  "B11"  "B12"  "B13"  "B02" 
3)gsubfn越来越短的解决方案包括提取数字,转换为数字,填充,然后使用gsubfn重新插入

library(gsubfn)
gsubfn("\\d+", ~ sprintf("%02d", as.numeric(digits)), x)
## [1] "MB01" "MB11" "MB12" "MB13" "B01"  "B11"  "B12"  "B13"  "B02" 
4)sub更短,但我们只能通过查找一位后缀并插入零来使用sub。没有使用任何软件包

sprintf("%s%02d", gsub("\\d", "", x), as.numeric(gsub("\\D", "", x)))
## [1] "MB01" "MB11" "MB12" "MB13" "B01"  "B11"  "B12"  "B13"  "B02" 
sub("(\\D)(\\d)$", "\\10\\2", x)
## [1] "MB01" "MB11" "MB12" "MB13" "B01"  "B11"  "B12"  "B13"  "B02" 
digits <- gsub("\\D", "", x)
nondigits <- gsub("\\d", "", x)
sprintf("%s%0*d", nondigits, max(nchar(digits)), as.numeric(digits))
## [1] "MB01" "MB11" "MB12" "MB13" "B01"  "B11"  "B12"  "B13"  "B02" 
5)泛化如果我们不知道涉及多少个数字,并且我们希望使用足够的0填充对它们进行右对齐,那么我们可以在(1)的这个变体中使用sprintf中的*格式。其他方面也可以采用同样的方法。没有使用任何软件包

sprintf("%s%02d", gsub("\\d", "", x), as.numeric(gsub("\\D", "", x)))
## [1] "MB01" "MB11" "MB12" "MB13" "B01"  "B11"  "B12"  "B13"  "B02" 
sub("(\\D)(\\d)$", "\\10\\2", x)
## [1] "MB01" "MB11" "MB12" "MB13" "B01"  "B11"  "B12"  "B13"  "B02" 
digits <- gsub("\\D", "", x)
nondigits <- gsub("\\d", "", x)
sprintf("%s%0*d", nondigits, max(nchar(digits)), as.numeric(digits))
## [1] "MB01" "MB11" "MB12" "MB13" "B01"  "B11"  "B12"  "B13"  "B02" 
digits1)gsub分别删除数字和非数字,然后使用sprintf组合回去。没有使用任何软件包

sprintf("%s%02d", gsub("\\d", "", x), as.numeric(gsub("\\D", "", x)))
## [1] "MB01" "MB11" "MB12" "MB13" "B01"  "B11"  "B12"  "B13"  "B02" 
sub("(\\D)(\\d)$", "\\10\\2", x)
## [1] "MB01" "MB11" "MB12" "MB13" "B01"  "B11"  "B12"  "B13"  "B02" 
digits <- gsub("\\D", "", x)
nondigits <- gsub("\\d", "", x)
sprintf("%s%0*d", nondigits, max(nchar(digits)), as.numeric(digits))
## [1] "MB01" "MB11" "MB12" "MB13" "B01"  "B11"  "B12"  "B13"  "B02" 
2)trimws使用trimws的此变体也仅使用基准R

sprintf("%s%02d", trimws(x,, "\\d"), as.numeric(trimws(x,, "\\D")))
## [1] "MB01" "MB11" "MB12" "MB13" "B01"  "B11"  "B12"  "B13"  "B02" 
3)gsubfn越来越短的解决方案包括提取数字,转换为数字,填充,然后使用gsubfn重新插入

library(gsubfn)
gsubfn("\\d+", ~ sprintf("%02d", as.numeric(digits)), x)
## [1] "MB01" "MB11" "MB12" "MB13" "B01"  "B11"  "B12"  "B13"  "B02" 
4)sub更短,但我们只能通过查找一位后缀并插入零来使用sub。没有使用任何软件包

sprintf("%s%02d", gsub("\\d", "", x), as.numeric(gsub("\\D", "", x)))
## [1] "MB01" "MB11" "MB12" "MB13" "B01"  "B11"  "B12"  "B13"  "B02" 
sub("(\\D)(\\d)$", "\\10\\2", x)
## [1] "MB01" "MB11" "MB12" "MB13" "B01"  "B11"  "B12"  "B13"  "B02" 
digits <- gsub("\\D", "", x)
nondigits <- gsub("\\d", "", x)
sprintf("%s%0*d", nondigits, max(nchar(digits)), as.numeric(digits))
## [1] "MB01" "MB11" "MB12" "MB13" "B01"  "B11"  "B12"  "B13"  "B02" 
5)泛化如果我们不知道涉及多少个数字,并且我们希望使用足够的0填充对它们进行右对齐,那么我们可以在(1)的这个变体中使用sprintf中的*格式。其他方面也可以采用同样的方法。没有使用任何软件包

sprintf("%s%02d", gsub("\\d", "", x), as.numeric(gsub("\\D", "", x)))
## [1] "MB01" "MB11" "MB12" "MB13" "B01"  "B11"  "B12"  "B13"  "B02" 
sub("(\\D)(\\d)$", "\\10\\2", x)
## [1] "MB01" "MB11" "MB12" "MB13" "B01"  "B11"  "B12"  "B13"  "B02" 
digits <- gsub("\\D", "", x)
nondigits <- gsub("\\d", "", x)
sprintf("%s%0*d", nondigits, max(nchar(digits)), as.numeric(digits))
## [1] "MB01" "MB11" "MB12" "MB13" "B01"  "B11"  "B12"  "B13"  "B02" 

digits使用
str_extract(x,[A-Z]+”
表示“在x的每个元素中找到的任何字母”,而
str_extract(x,[0-9]+”
表示“x的数字元素”,我想你的代码会工作的。工作得很有魅力!使用
str_extract(x,“[A-Z]+”)
表示“在x的每个元素中找到的任何字母”,使用
str_extract(x,[0-9]+”
表示“x的数字元素”,我想你的代码会工作的。工作得很有魅力!有人编辑了这个以更改缩进,结果是根据您的屏幕,一些代码可能会离开结尾。我已经还原了编辑之前的版本,以使其更易于阅读。有人编辑了此版本以更改缩进,其结果是,根据您的屏幕,部分代码可能会结束。我已经恢复了编辑之前的版本,以使其更易于阅读。