在R中按条件插入字符串
Insert a string by condition in R
我有这种情况,但有更多信息:
Barcode
NM00000512
NM000522
NM00000513
NM000514
我想在较短的条形码中再插入两个零,如下所示:
Barcode
NM00000512
NM00000522
NM00000513
NM00000514
我试过 df$Barcode <- gsub('NM000', "NM00000",df$Barcode)
但没有用。谢谢
对最初的尝试稍作修改:不是替换所有 NM000
,而是替换 NM000
仅当其后跟一个非 0 [^0]
的字符时,但确保捕获它()
然后用 \1
.
放回去
df$Barcode <- gsub('NM000([^0])', "NM00000\1",df$Barcode)
> df
# Barcode
# 1 NM00000512
# 2 NM00000522
# 3 NM00000513
# 4 NM00000514
或者使用相同的想法和负面前瞻:
gsub('NM000(?!=0)', "NM00000", df$Barcode, perl = TRUE)
可重现的数据:
df <- data.frame(
Barcode = c("NM00000512", "NM000522", "NM00000513", "NM000514"),
stringsAsFactors = FALSE
)
我们可以用5个0替换字符串中所有的0
sub("0+", "00000", df$Barcode)
#[1] "NM00000512" "NM00000522" "NM00000513" "NM00000514"
您可以使用
x <- c("NM00000512","NM000522","NM00000513","NM000514")
library(stringr)
str_replace(x, "(?<=^NM)\d+$", function(x) str_pad(x, 8, side = "left", pad = "0"))
## => [1] "NM00000512" "NM00000522" "NM00000513" "NM00000514"
如果从 NM
到字符串结尾最多可以有 8 位数字,请使用
str_replace(x, "(?<=^NM)\d{1,8}$", function(x) str_pad(x, 8, side = "left", pad = "0"))
## ^^^^^
详情
(?<=^NM)
- 匹配字符串 NM
开头的位置
\d{1,8}
- 1 到 8 位数字(\d+
匹配 1+ 位数字)
$
- 字符串结尾
str_pad(x, 8, side = "left", pad = "0")
用零填充左边的匹配数字。
其他答案对零的数量做出了假设(应该有五个)。我假设前面应该正好有两个字符。如果不是这种情况,您可以修改它以在第一个数字之前拆分字符串(或根据适用的任何不同规则)。
barcode <- c("NM00000512", "NM000522", "NM00000513", "NM000514")
pad <- function(barcode) {
x <- as.integer(substring(barcode, 3)) #extract numbers
n <- max(nchar(barcode)) - 2 #desired length of number in string
paste0(substring(barcode, 1, 2), #the two characters
sprintf(paste0("%0", n, "i"), x) #pad with zeros
)
}
pad(barcode)
#[1] "NM00000512" "NM00000522" "NM00000513" "NM00000514"
我有这种情况,但有更多信息:
Barcode
NM00000512
NM000522
NM00000513
NM000514
我想在较短的条形码中再插入两个零,如下所示:
Barcode
NM00000512
NM00000522
NM00000513
NM00000514
我试过 df$Barcode <- gsub('NM000', "NM00000",df$Barcode)
但没有用。谢谢
对最初的尝试稍作修改:不是替换所有 NM000
,而是替换 NM000
仅当其后跟一个非 0 [^0]
的字符时,但确保捕获它()
然后用 \1
.
df$Barcode <- gsub('NM000([^0])', "NM00000\1",df$Barcode)
> df
# Barcode
# 1 NM00000512
# 2 NM00000522
# 3 NM00000513
# 4 NM00000514
或者使用相同的想法和负面前瞻:
gsub('NM000(?!=0)', "NM00000", df$Barcode, perl = TRUE)
可重现的数据:
df <- data.frame(
Barcode = c("NM00000512", "NM000522", "NM00000513", "NM000514"),
stringsAsFactors = FALSE
)
我们可以用5个0替换字符串中所有的0
sub("0+", "00000", df$Barcode)
#[1] "NM00000512" "NM00000522" "NM00000513" "NM00000514"
您可以使用
x <- c("NM00000512","NM000522","NM00000513","NM000514")
library(stringr)
str_replace(x, "(?<=^NM)\d+$", function(x) str_pad(x, 8, side = "left", pad = "0"))
## => [1] "NM00000512" "NM00000522" "NM00000513" "NM00000514"
如果从 NM
到字符串结尾最多可以有 8 位数字,请使用
str_replace(x, "(?<=^NM)\d{1,8}$", function(x) str_pad(x, 8, side = "left", pad = "0"))
## ^^^^^
详情
(?<=^NM)
- 匹配字符串NM
开头的位置\d{1,8}
- 1 到 8 位数字(\d+
匹配 1+ 位数字)$
- 字符串结尾
str_pad(x, 8, side = "left", pad = "0")
用零填充左边的匹配数字。
其他答案对零的数量做出了假设(应该有五个)。我假设前面应该正好有两个字符。如果不是这种情况,您可以修改它以在第一个数字之前拆分字符串(或根据适用的任何不同规则)。
barcode <- c("NM00000512", "NM000522", "NM00000513", "NM000514")
pad <- function(barcode) {
x <- as.integer(substring(barcode, 3)) #extract numbers
n <- max(nchar(barcode)) - 2 #desired length of number in string
paste0(substring(barcode, 1, 2), #the two characters
sprintf(paste0("%0", n, "i"), x) #pad with zeros
)
}
pad(barcode)
#[1] "NM00000512" "NM00000522" "NM00000513" "NM00000514"