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;
}








10

















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)









share|improve this question

































    10

















    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)









    share|improve this question





























      10












      10








      10


      1






      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)









      share|improve this question














      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






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question



      share|improve this question










      asked May 27 at 12:31









      FatetaFateta

      1298 bronze badges




      1298 bronze badges



























          6 Answers
          6






          active

          oldest

          votes


















          12


















          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





          share|improve this answer




































            3


















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





            share|improve this answer


































              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])





              share|improve this answer





























              • @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



















              2


















              Meanwhile, I came up with another base R solution



              lower <- as.numeric(sub(".]*", "", sub(";.*", "", confint)))
              upper <- as.numeric(sub("].*", "", sub(".*;", "", confint)))


              Thank you all!






              share|improve this answer




































                1


















                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





                share|improve this answer




































                  1


















                  A tidyverse solution:



                  library(dplyr)
                  library(tidyr)

                  df = data.frame(confint)
                  df = df %>%
                  mutate(confint = gsub("(\[|\])","",confint)) %>%
                  separate(confint,c("lower","upper"),";",convert=T)





                  share|improve this answer




























                    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
                    });


                    }
                    });















                    draft saved

                    draft discarded
















                    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









                    12


















                    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





                    share|improve this answer

































                      12


















                      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





                      share|improve this answer































                        12














                        12










                        12









                        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





                        share|improve this answer
















                        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






                        share|improve this answer















                        share|improve this answer




                        share|improve this answer



                        share|improve this answer








                        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




























                            3


















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





                            share|improve this answer































                              3


















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





                              share|improve this answer





























                                3














                                3










                                3









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





                                share|improve this answer














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






                                share|improve this answer













                                share|improve this answer




                                share|improve this answer



                                share|improve this answer










                                answered May 27 at 12:52









                                pzhaopzhao

                                2401 silver badge8 bronze badges




                                2401 silver badge8 bronze badges


























                                    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])





                                    share|improve this answer





























                                    • @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
















                                    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])





                                    share|improve this answer





























                                    • @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














                                    2














                                    2










                                    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])





                                    share|improve this answer
















                                    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])






                                    share|improve this answer















                                    share|improve this answer




                                    share|improve this answer



                                    share|improve this answer








                                    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



















                                    • @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











                                    2


















                                    Meanwhile, I came up with another base R solution



                                    lower <- as.numeric(sub(".]*", "", sub(";.*", "", confint)))
                                    upper <- as.numeric(sub("].*", "", sub(".*;", "", confint)))


                                    Thank you all!






                                    share|improve this answer

































                                      2


















                                      Meanwhile, I came up with another base R solution



                                      lower <- as.numeric(sub(".]*", "", sub(";.*", "", confint)))
                                      upper <- as.numeric(sub("].*", "", sub(".*;", "", confint)))


                                      Thank you all!






                                      share|improve this answer































                                        2














                                        2










                                        2









                                        Meanwhile, I came up with another base R solution



                                        lower <- as.numeric(sub(".]*", "", sub(";.*", "", confint)))
                                        upper <- as.numeric(sub("].*", "", sub(".*;", "", confint)))


                                        Thank you all!






                                        share|improve this answer
















                                        Meanwhile, I came up with another base R solution



                                        lower <- as.numeric(sub(".]*", "", sub(";.*", "", confint)))
                                        upper <- as.numeric(sub("].*", "", sub(".*;", "", confint)))


                                        Thank you all!







                                        share|improve this answer















                                        share|improve this answer




                                        share|improve this answer



                                        share|improve this answer








                                        edited Jun 11 at 5:31

























                                        answered May 27 at 13:22









                                        FatetaFateta

                                        1298 bronze badges




                                        1298 bronze badges


























                                            1


















                                            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





                                            share|improve this answer

































                                              1


















                                              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





                                              share|improve this answer































                                                1














                                                1










                                                1









                                                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





                                                share|improve this answer
















                                                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






                                                share|improve this answer















                                                share|improve this answer




                                                share|improve this answer



                                                share|improve this answer








                                                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


























                                                    1


















                                                    A tidyverse solution:



                                                    library(dplyr)
                                                    library(tidyr)

                                                    df = data.frame(confint)
                                                    df = df %>%
                                                    mutate(confint = gsub("(\[|\])","",confint)) %>%
                                                    separate(confint,c("lower","upper"),";",convert=T)





                                                    share|improve this answer































                                                      1


















                                                      A tidyverse solution:



                                                      library(dplyr)
                                                      library(tidyr)

                                                      df = data.frame(confint)
                                                      df = df %>%
                                                      mutate(confint = gsub("(\[|\])","",confint)) %>%
                                                      separate(confint,c("lower","upper"),";",convert=T)





                                                      share|improve this answer





























                                                        1














                                                        1










                                                        1









                                                        A tidyverse solution:



                                                        library(dplyr)
                                                        library(tidyr)

                                                        df = data.frame(confint)
                                                        df = df %>%
                                                        mutate(confint = gsub("(\[|\])","",confint)) %>%
                                                        separate(confint,c("lower","upper"),";",convert=T)





                                                        share|improve this answer














                                                        A tidyverse solution:



                                                        library(dplyr)
                                                        library(tidyr)

                                                        df = data.frame(confint)
                                                        df = df %>%
                                                        mutate(confint = gsub("(\[|\])","",confint)) %>%
                                                        separate(confint,c("lower","upper"),";",convert=T)






                                                        share|improve this answer













                                                        share|improve this answer




                                                        share|improve this answer



                                                        share|improve this answer










                                                        answered May 27 at 14:12









                                                        FinoFino

                                                        1,2405 silver badges16 bronze badges




                                                        1,2405 silver badges16 bronze badges


































                                                            draft saved

                                                            draft discarded



















































                                                            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.




                                                            draft saved


                                                            draft discarded














                                                            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





















































                                                            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







                                                            Popular posts from this blog

                                                            He _____ here since 1970 . Answer needed [closed]What does “since he was so high” mean?Meaning of “catch birds for”?How do I ensure “since” takes the meaning I want?“Who cares here” meaningWhat does “right round toward” mean?the time tense (had now been detected)What does the phrase “ring around the roses” mean here?Correct usage of “visited upon”Meaning of “foiled rail sabotage bid”It was the third time I had gone to Rome or It is the third time I had been to Rome

                                                            Bunad

                                                            Færeyskur hestur Heimild | Tengill | Tilvísanir | LeiðsagnarvalRossið - síða um færeyska hrossið á færeyskuGott ár hjá færeyska hestinum