R program questions










0















I am trying to get some unique combinations of two variables.

For each value of x, I would like to have this unique y value, and drop those have several y values. But several x values could share same y value.

For example,
a=data.frame(x=c(1,1,2,4,5,5),y=c(2,3,3,3,6,6)),


and I would like to get the output like:



b=data.frame(x=c(2,4,5),y=c(3,3,6))



I have tried unique(), but it does not help this situation.
Thank you!










share|improve this question




























    0















    I am trying to get some unique combinations of two variables.

    For each value of x, I would like to have this unique y value, and drop those have several y values. But several x values could share same y value.

    For example,
    a=data.frame(x=c(1,1,2,4,5,5),y=c(2,3,3,3,6,6)),


    and I would like to get the output like:



    b=data.frame(x=c(2,4,5),y=c(3,3,6))



    I have tried unique(), but it does not help this situation.
    Thank you!










    share|improve this question


























      0












      0








      0








      I am trying to get some unique combinations of two variables.

      For each value of x, I would like to have this unique y value, and drop those have several y values. But several x values could share same y value.

      For example,
      a=data.frame(x=c(1,1,2,4,5,5),y=c(2,3,3,3,6,6)),


      and I would like to get the output like:



      b=data.frame(x=c(2,4,5),y=c(3,3,6))



      I have tried unique(), but it does not help this situation.
      Thank you!










      share|improve this question
















      I am trying to get some unique combinations of two variables.

      For each value of x, I would like to have this unique y value, and drop those have several y values. But several x values could share same y value.

      For example,
      a=data.frame(x=c(1,1,2,4,5,5),y=c(2,3,3,3,6,6)),


      and I would like to get the output like:



      b=data.frame(x=c(2,4,5),y=c(3,3,6))



      I have tried unique(), but it does not help this situation.
      Thank you!







      r






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 14 '18 at 19:16









      joran

      136k19328388




      136k19328388










      asked Nov 14 '18 at 19:10









      Lilia FengLilia Feng

      374




      374






















          2 Answers
          2






          active

          oldest

          votes


















          1














          First we use unique to omit repeated rows with the same x and y values (keeping only one copy of each). Any repeated x values that are left have different y values, so we want to get rid of them. We use the standard way to remove all copies of any duplicated values as in this R-FAQ.



          a=data.frame(x=c(1,1,2,4,5,5),y=c(2,3,3,3,6,6))
          b = unique(a)
          b = b[!duplicated(b$x) & !duplicated(b$x, fromLast = TRUE), ]
          b
          # x y
          # 3 2 3
          # 4 4 3
          # 5 5 6


          Fans of dplyr would probably do it like this, producing the same result.



          library(dplyr)
          a %>%
          group_by(x) %>%
          filter(n_distinct(y) == 1) %>%
          distinct





          share|improve this answer
































            1














            Using dplyr:





            library(dplyr)

            a <- data.frame(x=c(1,1,2,4,5,5),y=c(2,3,3,3,6,6))

            a %>%
            distinct() %>%
            add_count(x) %>% # adds an implicit group_by(x)
            filter(n == 1) %>%
            select(-n)
            #> # A tibble: 3 x 2
            #> # Groups: x [3]
            #> x y
            #> <dbl> <dbl>
            #> 1 2 3
            #> 2 4 3
            #> 3 5 6


            Created on 2018-11-14 by the reprex package (v0.2.1)






            share|improve this answer

























            • Good point! Will make the edit.

              – amanda
              Nov 14 '18 at 20:33










            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/3.0/"u003ecc by-sa 3.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%2f53307198%2fr-program-questions%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            1














            First we use unique to omit repeated rows with the same x and y values (keeping only one copy of each). Any repeated x values that are left have different y values, so we want to get rid of them. We use the standard way to remove all copies of any duplicated values as in this R-FAQ.



            a=data.frame(x=c(1,1,2,4,5,5),y=c(2,3,3,3,6,6))
            b = unique(a)
            b = b[!duplicated(b$x) & !duplicated(b$x, fromLast = TRUE), ]
            b
            # x y
            # 3 2 3
            # 4 4 3
            # 5 5 6


            Fans of dplyr would probably do it like this, producing the same result.



            library(dplyr)
            a %>%
            group_by(x) %>%
            filter(n_distinct(y) == 1) %>%
            distinct





            share|improve this answer





























              1














              First we use unique to omit repeated rows with the same x and y values (keeping only one copy of each). Any repeated x values that are left have different y values, so we want to get rid of them. We use the standard way to remove all copies of any duplicated values as in this R-FAQ.



              a=data.frame(x=c(1,1,2,4,5,5),y=c(2,3,3,3,6,6))
              b = unique(a)
              b = b[!duplicated(b$x) & !duplicated(b$x, fromLast = TRUE), ]
              b
              # x y
              # 3 2 3
              # 4 4 3
              # 5 5 6


              Fans of dplyr would probably do it like this, producing the same result.



              library(dplyr)
              a %>%
              group_by(x) %>%
              filter(n_distinct(y) == 1) %>%
              distinct





              share|improve this answer



























                1












                1








                1







                First we use unique to omit repeated rows with the same x and y values (keeping only one copy of each). Any repeated x values that are left have different y values, so we want to get rid of them. We use the standard way to remove all copies of any duplicated values as in this R-FAQ.



                a=data.frame(x=c(1,1,2,4,5,5),y=c(2,3,3,3,6,6))
                b = unique(a)
                b = b[!duplicated(b$x) & !duplicated(b$x, fromLast = TRUE), ]
                b
                # x y
                # 3 2 3
                # 4 4 3
                # 5 5 6


                Fans of dplyr would probably do it like this, producing the same result.



                library(dplyr)
                a %>%
                group_by(x) %>%
                filter(n_distinct(y) == 1) %>%
                distinct





                share|improve this answer















                First we use unique to omit repeated rows with the same x and y values (keeping only one copy of each). Any repeated x values that are left have different y values, so we want to get rid of them. We use the standard way to remove all copies of any duplicated values as in this R-FAQ.



                a=data.frame(x=c(1,1,2,4,5,5),y=c(2,3,3,3,6,6))
                b = unique(a)
                b = b[!duplicated(b$x) & !duplicated(b$x, fromLast = TRUE), ]
                b
                # x y
                # 3 2 3
                # 4 4 3
                # 5 5 6


                Fans of dplyr would probably do it like this, producing the same result.



                library(dplyr)
                a %>%
                group_by(x) %>%
                filter(n_distinct(y) == 1) %>%
                distinct






                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Nov 14 '18 at 19:29

























                answered Nov 14 '18 at 19:23









                GregorGregor

                66.8k1092177




                66.8k1092177























                    1














                    Using dplyr:





                    library(dplyr)

                    a <- data.frame(x=c(1,1,2,4,5,5),y=c(2,3,3,3,6,6))

                    a %>%
                    distinct() %>%
                    add_count(x) %>% # adds an implicit group_by(x)
                    filter(n == 1) %>%
                    select(-n)
                    #> # A tibble: 3 x 2
                    #> # Groups: x [3]
                    #> x y
                    #> <dbl> <dbl>
                    #> 1 2 3
                    #> 2 4 3
                    #> 3 5 6


                    Created on 2018-11-14 by the reprex package (v0.2.1)






                    share|improve this answer

























                    • Good point! Will make the edit.

                      – amanda
                      Nov 14 '18 at 20:33















                    1














                    Using dplyr:





                    library(dplyr)

                    a <- data.frame(x=c(1,1,2,4,5,5),y=c(2,3,3,3,6,6))

                    a %>%
                    distinct() %>%
                    add_count(x) %>% # adds an implicit group_by(x)
                    filter(n == 1) %>%
                    select(-n)
                    #> # A tibble: 3 x 2
                    #> # Groups: x [3]
                    #> x y
                    #> <dbl> <dbl>
                    #> 1 2 3
                    #> 2 4 3
                    #> 3 5 6


                    Created on 2018-11-14 by the reprex package (v0.2.1)






                    share|improve this answer

























                    • Good point! Will make the edit.

                      – amanda
                      Nov 14 '18 at 20:33













                    1












                    1








                    1







                    Using dplyr:





                    library(dplyr)

                    a <- data.frame(x=c(1,1,2,4,5,5),y=c(2,3,3,3,6,6))

                    a %>%
                    distinct() %>%
                    add_count(x) %>% # adds an implicit group_by(x)
                    filter(n == 1) %>%
                    select(-n)
                    #> # A tibble: 3 x 2
                    #> # Groups: x [3]
                    #> x y
                    #> <dbl> <dbl>
                    #> 1 2 3
                    #> 2 4 3
                    #> 3 5 6


                    Created on 2018-11-14 by the reprex package (v0.2.1)






                    share|improve this answer















                    Using dplyr:





                    library(dplyr)

                    a <- data.frame(x=c(1,1,2,4,5,5),y=c(2,3,3,3,6,6))

                    a %>%
                    distinct() %>%
                    add_count(x) %>% # adds an implicit group_by(x)
                    filter(n == 1) %>%
                    select(-n)
                    #> # A tibble: 3 x 2
                    #> # Groups: x [3]
                    #> x y
                    #> <dbl> <dbl>
                    #> 1 2 3
                    #> 2 4 3
                    #> 3 5 6


                    Created on 2018-11-14 by the reprex package (v0.2.1)







                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Nov 14 '18 at 20:34

























                    answered Nov 14 '18 at 19:28









                    amandaamanda

                    111411




                    111411












                    • Good point! Will make the edit.

                      – amanda
                      Nov 14 '18 at 20:33

















                    • Good point! Will make the edit.

                      – amanda
                      Nov 14 '18 at 20:33
















                    Good point! Will make the edit.

                    – amanda
                    Nov 14 '18 at 20:33





                    Good point! Will make the edit.

                    – amanda
                    Nov 14 '18 at 20:33

















                    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%2f53307198%2fr-program-questions%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

                    Use pre created SQLite database for Android project in kotlin

                    Darth Vader #20

                    Ondo