gpt4 book ai didi

r - 提取模式 "number/number"

转载 作者:行者123 更新时间:2023-12-04 16:07:16 27 4
gpt4 key购买 nike

我想从字符串中提取血压。数据可能如下所示:

text <- c("at 10.00 seated 132/69", "99/49", "176/109",  
"10.12 I 128/51, II 149/51 h.9.16", "153/82 p.90 ja 154/81 p.86",
"h:17.45", "not measured", "time 7.30 RR 202/97 p. 69")

我想提取模式“number/number”(即“132/69”)。在上面的示例中,预期输出将是一个列表:

[[1]]
[1] "132/69"

[[2]]
[1] "99/49"

[[3]]
[1] "176/109"

[[4]]
[1] "128/51" "149/51"

[[5]]
[1] "153/82" "154/81"

[[6]]
[1] ""

[[7]]
[1] ""

[[8]]
[1] "202/97"

我拥有的最接近的解决方案:

gsub( "^.*([0-9]{3}/[0-9]+).*","\\1", text)

不幸的是,在我的解决方案中,它不会返回该模式的所有匹配情况,并且还返回一个根本不具有所需模式的字符串。

最佳答案

regmatches(text, gregexpr("\\d+/\\d+", text))
#[[1]]
#[1] "132/69"
#
#[[2]]
#[1] "99/49"
#
#[[3]]
#[1] "176/109"
#
#[[4]]
#[1] "128/51" "149/51"
#
#[[5]]
#[1] "153/82" "154/81"
#
#[[6]]
#character(0)
#
#[[7]]
#character(0)
#
#[[8]]
#[1] "202/97"

关于r - 提取模式 "number/number",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40021396/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com