希望这有意义。
我有一串不同长度的p值(由于四舍五入),其中非常大和小的p值存储为1.0(some number of 0s corresponding to the length of rounding)
,和0.0(some number of 0s corresponding to the length of rounding)
, 分别。
我想匹配两组模式:
第一:
"(1.)(string of zeros of any length)"
并将其更改为 "> 0.(sting of nines the same length as the string of zeros)"
第二
"(0.)(string of zeros of any length)"
并将其更改为 "< 0.(string of zeros the length of the input minus one)1
.
因此,如果我们有以下输入:
pvals<-c("1.000","1.00","0.00000","0.123","0.6","0.0")
我希望返回:
> expectedOutput
[1] "> 0.999" "> 0.99" "< 0.00001" "0.123" "0.6" "< 0.1"
我一直在尝试使用gsub,但我对正则表达式的更复杂使用知之甚少,我不明白如何允许某个字符(0)的任意长度,然后如何替换为相同数量的新字符(在 1.0s 的情况下),或该数字减 1(在 0.0s 的情况下)
任何帮助将不胜感激! 谢谢
最佳答案
你可以做这样的事情
> pvals<-c("1.000","1.00","0.00000","0.123","0.6","0.0")
> x <- gsub("(?:^1\\.|\\G)\\K0(?=0*$)", "9", pvals, perl=T)
> m <- gsub("^1\\.", "> 0.", x)
> gsub("^(0\\.0*)0$", "< \\11", m)
[1] "> 0.999" "> 0.99" "< 0.00001" "0.123"
[5] "0.6" "< 0.1"
关于regex - 正则表达式匹配和替换 pvalue 字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29344262/