How to extract lower and upper bound in numeric format from a confidence interval string?

A base R solution

lower =  as.numeric(sub(".*?(\\d+\\.\\d+).*", "\\1", confint))
upper =  as.numeric(sub(".*\\b(\\d+\\.\\d+).*", "\\1", confint))

lower
[1]   0.741 263.917  12.788   0.680   0.650   0.719
upper
[1] 2.233 402.154  17.975   2.450   1.827   2.190

mypattern <- '\\[(\\d+\\.\\d+) ; (\\d+\\.\\d+)\\]'
as.numeric(gsub(mypattern, '\\1', confint))
as.numeric(gsub(mypattern, '\\2', confint))

A different base R possibility could be:

sapply(strsplit(confint, " ; ", fixed = TRUE), function(x) gsub("[^0-9.-]+", "\\1", x) [1])
sapply(strsplit(confint, " ; ", fixed = TRUE), function(x) gsub("[^0-9.-]+", "\\1", x) [2])

[1] "0.741"   "263.917" "12.788"  "0.680"   "0.650"   "0.719"  
[1] "2.233"   "402.154" "17.975"  "2.450"   "1.827"   "2.190" 

If you need it as a numeric vector:

sapply(strsplit(confint, " ; ", fixed = TRUE), function(x) as.numeric(gsub("[^0-9.-]+", "\\1", x)) [1])
sapply(strsplit(confint, " ; ", fixed = TRUE), function(x) as.numeric(gsub("[^0-9.-]+", "\\1", x)) [2])

Tags:

R