Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

r

Suppose a vector including some confidence intervals as below

confint <- c("[0.741 ; 2.233]", "[263.917 ; 402.154]", "[12.788 ; 17.975]", "[0.680 ; 2.450]", "[0.650 ; 1.827]", "[0.719 ; 2.190]")

I want to have two new vectors one including the lower Limits in numeric format as

lower <- c(0.741, 263.917, 12.788, 0.680, 0.650 , 0.719)

and othe including the upper Limits in numeric format like

upper <- c(2.233, 402.154, 17.975, 2.450, 1.827, 2.190)
like image 580
Fateta Avatar asked May 27 '19 12:05

Fateta


2 Answers

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
like image 157
G5W Avatar answered Sep 28 '22 15:09

G5W


mypattern <- '\\[(\\d+\\.\\d+) ; (\\d+\\.\\d+)\\]'
as.numeric(gsub(mypattern, '\\1', confint))
as.numeric(gsub(mypattern, '\\2', confint))
like image 44
pzhao Avatar answered Sep 28 '22 15:09

pzhao