How to extract lower and upper bound in numeric format from a confidence interval string?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{
margin-bottom:0;
}
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)
r
add a comment
|
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)
r
add a comment
|
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)
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)
r
r
asked May 27 at 12:31
FatetaFateta
1298 bronze badges
1298 bronze badges
add a comment
|
add a comment
|
6 Answers
6
active
oldest
votes
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
add a comment
|
mypattern <- '\[(\d+\.\d+) ; (\d+\.\d+)\]'
as.numeric(gsub(mypattern, '\1', confint))
as.numeric(gsub(mypattern, '\2', confint))
add a comment
|
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])
@Ronak Shah I updated my post according some of your ideas. Thank you :)
– tmfmnk
May 27 at 12:59
I realised it needed some more tweaking to get exact output but I think it was a similar approach. +1 to you.
– Ronak Shah
May 27 at 13:21
add a comment
|
Meanwhile, I came up with another base R
solution
lower <- as.numeric(sub(".]*", "", sub(";.*", "", confint)))
upper <- as.numeric(sub("].*", "", sub(".*;", "", confint)))
Thank you all!
add a comment
|
You can use functions from the stringr library.
You can split strings with str_split()
according a specific character (;
in your case), then remove character with str_remove()
([
and ]
in your case) and you will obtain what you want.
str_remove(str_split_fixed(confint, ";", n = 2)[,1], '\[') %>% as.numeric()
# [1] 0.741 263.917 12.788 0.680 0.650 0.719
add a comment
|
A tidyverse
solution:
library(dplyr)
library(tidyr)
df = data.frame(confint)
df = df %>%
mutate(confint = gsub("(\[|\])","",confint)) %>%
separate(confint,c("lower","upper"),";",convert=T)
add a comment
|
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/4.0/"u003ecc by-sa 4.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f56326130%2fhow-to-extract-lower-and-upper-bound-in-numeric-format-from-a-confidence-interva%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
6 Answers
6
active
oldest
votes
6 Answers
6
active
oldest
votes
active
oldest
votes
active
oldest
votes
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
add a comment
|
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
add a comment
|
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
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
edited May 27 at 12:45
answered May 27 at 12:41
G5WG5W
25.7k9 gold badges24 silver badges46 bronze badges
25.7k9 gold badges24 silver badges46 bronze badges
add a comment
|
add a comment
|
mypattern <- '\[(\d+\.\d+) ; (\d+\.\d+)\]'
as.numeric(gsub(mypattern, '\1', confint))
as.numeric(gsub(mypattern, '\2', confint))
add a comment
|
mypattern <- '\[(\d+\.\d+) ; (\d+\.\d+)\]'
as.numeric(gsub(mypattern, '\1', confint))
as.numeric(gsub(mypattern, '\2', confint))
add a comment
|
mypattern <- '\[(\d+\.\d+) ; (\d+\.\d+)\]'
as.numeric(gsub(mypattern, '\1', confint))
as.numeric(gsub(mypattern, '\2', confint))
mypattern <- '\[(\d+\.\d+) ; (\d+\.\d+)\]'
as.numeric(gsub(mypattern, '\1', confint))
as.numeric(gsub(mypattern, '\2', confint))
answered May 27 at 12:52
pzhaopzhao
2401 silver badge8 bronze badges
2401 silver badge8 bronze badges
add a comment
|
add a comment
|
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])
@Ronak Shah I updated my post according some of your ideas. Thank you :)
– tmfmnk
May 27 at 12:59
I realised it needed some more tweaking to get exact output but I think it was a similar approach. +1 to you.
– Ronak Shah
May 27 at 13:21
add a comment
|
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])
@Ronak Shah I updated my post according some of your ideas. Thank you :)
– tmfmnk
May 27 at 12:59
I realised it needed some more tweaking to get exact output but I think it was a similar approach. +1 to you.
– Ronak Shah
May 27 at 13:21
add a comment
|
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])
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])
edited May 27 at 12:58
answered May 27 at 12:49
tmfmnktmfmnk
12.1k1 gold badge10 silver badges25 bronze badges
12.1k1 gold badge10 silver badges25 bronze badges
@Ronak Shah I updated my post according some of your ideas. Thank you :)
– tmfmnk
May 27 at 12:59
I realised it needed some more tweaking to get exact output but I think it was a similar approach. +1 to you.
– Ronak Shah
May 27 at 13:21
add a comment
|
@Ronak Shah I updated my post according some of your ideas. Thank you :)
– tmfmnk
May 27 at 12:59
I realised it needed some more tweaking to get exact output but I think it was a similar approach. +1 to you.
– Ronak Shah
May 27 at 13:21
@Ronak Shah I updated my post according some of your ideas. Thank you :)
– tmfmnk
May 27 at 12:59
@Ronak Shah I updated my post according some of your ideas. Thank you :)
– tmfmnk
May 27 at 12:59
I realised it needed some more tweaking to get exact output but I think it was a similar approach. +1 to you.
– Ronak Shah
May 27 at 13:21
I realised it needed some more tweaking to get exact output but I think it was a similar approach. +1 to you.
– Ronak Shah
May 27 at 13:21
add a comment
|
Meanwhile, I came up with another base R
solution
lower <- as.numeric(sub(".]*", "", sub(";.*", "", confint)))
upper <- as.numeric(sub("].*", "", sub(".*;", "", confint)))
Thank you all!
add a comment
|
Meanwhile, I came up with another base R
solution
lower <- as.numeric(sub(".]*", "", sub(";.*", "", confint)))
upper <- as.numeric(sub("].*", "", sub(".*;", "", confint)))
Thank you all!
add a comment
|
Meanwhile, I came up with another base R
solution
lower <- as.numeric(sub(".]*", "", sub(";.*", "", confint)))
upper <- as.numeric(sub("].*", "", sub(".*;", "", confint)))
Thank you all!
Meanwhile, I came up with another base R
solution
lower <- as.numeric(sub(".]*", "", sub(";.*", "", confint)))
upper <- as.numeric(sub("].*", "", sub(".*;", "", confint)))
Thank you all!
edited Jun 11 at 5:31
answered May 27 at 13:22
FatetaFateta
1298 bronze badges
1298 bronze badges
add a comment
|
add a comment
|
You can use functions from the stringr library.
You can split strings with str_split()
according a specific character (;
in your case), then remove character with str_remove()
([
and ]
in your case) and you will obtain what you want.
str_remove(str_split_fixed(confint, ";", n = 2)[,1], '\[') %>% as.numeric()
# [1] 0.741 263.917 12.788 0.680 0.650 0.719
add a comment
|
You can use functions from the stringr library.
You can split strings with str_split()
according a specific character (;
in your case), then remove character with str_remove()
([
and ]
in your case) and you will obtain what you want.
str_remove(str_split_fixed(confint, ";", n = 2)[,1], '\[') %>% as.numeric()
# [1] 0.741 263.917 12.788 0.680 0.650 0.719
add a comment
|
You can use functions from the stringr library.
You can split strings with str_split()
according a specific character (;
in your case), then remove character with str_remove()
([
and ]
in your case) and you will obtain what you want.
str_remove(str_split_fixed(confint, ";", n = 2)[,1], '\[') %>% as.numeric()
# [1] 0.741 263.917 12.788 0.680 0.650 0.719
You can use functions from the stringr library.
You can split strings with str_split()
according a specific character (;
in your case), then remove character with str_remove()
([
and ]
in your case) and you will obtain what you want.
str_remove(str_split_fixed(confint, ";", n = 2)[,1], '\[') %>% as.numeric()
# [1] 0.741 263.917 12.788 0.680 0.650 0.719
edited May 27 at 12:45
answered May 27 at 12:38
demarsylvaindemarsylvain
1,5142 gold badges7 silver badges21 bronze badges
1,5142 gold badges7 silver badges21 bronze badges
add a comment
|
add a comment
|
A tidyverse
solution:
library(dplyr)
library(tidyr)
df = data.frame(confint)
df = df %>%
mutate(confint = gsub("(\[|\])","",confint)) %>%
separate(confint,c("lower","upper"),";",convert=T)
add a comment
|
A tidyverse
solution:
library(dplyr)
library(tidyr)
df = data.frame(confint)
df = df %>%
mutate(confint = gsub("(\[|\])","",confint)) %>%
separate(confint,c("lower","upper"),";",convert=T)
add a comment
|
A tidyverse
solution:
library(dplyr)
library(tidyr)
df = data.frame(confint)
df = df %>%
mutate(confint = gsub("(\[|\])","",confint)) %>%
separate(confint,c("lower","upper"),";",convert=T)
A tidyverse
solution:
library(dplyr)
library(tidyr)
df = data.frame(confint)
df = df %>%
mutate(confint = gsub("(\[|\])","",confint)) %>%
separate(confint,c("lower","upper"),";",convert=T)
answered May 27 at 14:12
FinoFino
1,2405 silver badges16 bronze badges
1,2405 silver badges16 bronze badges
add a comment
|
add a comment
|
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f56326130%2fhow-to-extract-lower-and-upper-bound-in-numeric-format-from-a-confidence-interva%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown