将 _ 添加到 R 中的多个元素

Add _ to multiple elements in R

我有数百个变量名,如下所示:

c("B02001001","B02001002","B23020003","B19057001")

但我需要它们看起来像这样:

"B02001_001","B02001_002","B23020_003","B19057_001"

我假设有一种简单的方法可以做到这一点...也许是 stringr?任何想法将不胜感激。谢谢!

看起来你可以做到

x <- c("B02001001","B02001002","B23020003","B19057001")

paste(substr(x, 1L, 6L), substr(x, 7L, 1e5L), sep = "_")
# [1] "B02001_001" "B02001_002" "B23020_003" "B19057_001"

1e5L 仅作为安全预防措施存在,以防某些字符串长于 9 个字符。如果您知道它们都是 9,只需将其替换为 9L。您也可以使用 substring() 作为第二个调用而不必担心。

paste(substr(x, 1L, 6L), substring(x, 7L), sep = "_")
# [1] "B02001_001" "B02001_002" "B23020_003" "B19057_001"

我们可以将 sub 与捕获组一起使用。我们将第一组捕获为 6 个字符 ((.{6})),然后是字符,直到字符串的末尾作为第二组。在替换中,我们可以在第一个 (\1) 和第二个 (\2) 捕获组之间使用 _

sub('(.{6})(.*)$', '\1_\2', v1)
#[1] "B02001_001" "B02001_002" "B23020_003" "B19057_001"

数据

v1 <- c("B02001001","B02001002","B23020003","B19057001")