我有一个 3 行 x 1 列向量,其值为:“S0027A-E”、“S0028A-D”、“S0029A-C”;因此:
input_string <- as.vector(c("S0027A-E", "S0028A-D", "S0029A-C"))
输出的解析字符串必须是一个用逗号分隔的列表,对应于输入向量中的每个值,这样:
输入字符串 | 解析的字符串 |
---|---|
“S0027A-E” | “S0027A”、“S0027B”、“S0027C”、“S0027D”、“S0027E” |
“S0028A-D” | “S0028A”、“S0028B”、“S0028C”、“S0028D” |
“S0029A-C” | “S0029A”、“S0029B”、“S0029C” |
我已经最初创建了解析脚本,但输出错误地是包含 12 个元素的 1 行向量: "S0027A" "S0027B" "S0027C" "S0027D" "S0027E" "S0028A" "S0028B" "S0028C" "S0028D" “S0029A”“S0029B”“S0029C”-全部在一行中,而不是表中显示的输出。
# length of input_string
len_string = length(input_string)
# Extract the prefix, start, and end letters
library(stringr)
parsed_strings <- as.character()
for (i in 1:len_string){
prefix <- str_extract(input_string[[i]][1], "^[A-Z]\\d{4}")
range_part <- str_extract(input_string[[i]][1], "[A-Z]-[A-Z]$")
start_letter <- substr(range_part, 1, 1)
end_letter <- substr(range_part, 3, 3)
output <- paste0(prefix, LETTERS[match(start_letter, LETTERS):match(end_letter, LETTERS)])
parsed_strings <- c(parsed_strings, output)
}
输出必须如表中所示,非常感谢任何纠正我的代码的建议。预先感谢!
你可以试试
lapply(
strsplit(input_string, split = "(?<=\\d)(?=\\D)|-", perl = TRUE),
\(x) {
paste0(x[1], LETTERS[LETTERS >= x[2] & LETTERS <= x[3]])
}
)
这给出了
[[1]]
[1] "S0027A" "S0027B" "S0027C" "S0027D" "S0027E"
[[2]]
[1] "S0028A" "S0028B" "S0028C" "S0028D"
[[3]]
[1] "S0029A" "S0029B" "S0029C"