How to find a column value based on the unique value for another column












1















I have the following dataset:



Class     Value
A 5.4
A 5.4
A 5.4
B 3.6
B 2.7
C 4.02
C 4.02
C 4.02
D 6.33
D 6.33


What I want is to retrieve only the classes that have similar values, which in this case should return the class A and D but not, for example, the class B since it has two different values.



To do that, I tried the following:



sub <- dataset[as.logical(ave(dataset$Value, dataset$Class, FUN = function(x) all(x==x))), ]


But this returns all the classes which I don't want.



Can someone help me with that?










share|improve this question



























    1















    I have the following dataset:



    Class     Value
    A 5.4
    A 5.4
    A 5.4
    B 3.6
    B 2.7
    C 4.02
    C 4.02
    C 4.02
    D 6.33
    D 6.33


    What I want is to retrieve only the classes that have similar values, which in this case should return the class A and D but not, for example, the class B since it has two different values.



    To do that, I tried the following:



    sub <- dataset[as.logical(ave(dataset$Value, dataset$Class, FUN = function(x) all(x==x))), ]


    But this returns all the classes which I don't want.



    Can someone help me with that?










    share|improve this question

























      1












      1








      1








      I have the following dataset:



      Class     Value
      A 5.4
      A 5.4
      A 5.4
      B 3.6
      B 2.7
      C 4.02
      C 4.02
      C 4.02
      D 6.33
      D 6.33


      What I want is to retrieve only the classes that have similar values, which in this case should return the class A and D but not, for example, the class B since it has two different values.



      To do that, I tried the following:



      sub <- dataset[as.logical(ave(dataset$Value, dataset$Class, FUN = function(x) all(x==x))), ]


      But this returns all the classes which I don't want.



      Can someone help me with that?










      share|improve this question














      I have the following dataset:



      Class     Value
      A 5.4
      A 5.4
      A 5.4
      B 3.6
      B 2.7
      C 4.02
      C 4.02
      C 4.02
      D 6.33
      D 6.33


      What I want is to retrieve only the classes that have similar values, which in this case should return the class A and D but not, for example, the class B since it has two different values.



      To do that, I tried the following:



      sub <- dataset[as.logical(ave(dataset$Value, dataset$Class, FUN = function(x) all(x==x))), ]


      But this returns all the classes which I don't want.



      Can someone help me with that?







      r






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Jan 18 at 20:09









      Adam AminAdam Amin

      31018




      31018
























          2 Answers
          2






          active

          oldest

          votes


















          3














          Using aggregate with number of unique (length(unique))



          filterdf=aggregate(Value ~ Class, df, function(x) length(unique(x)))
          df[df$Class%in%filterdf[filterdf$Value==1,]$Class,]
          Class Value
          1 A 5.40
          2 A 5.40
          3 A 5.40
          6 C 4.02
          7 C 4.02
          8 C 4.02
          9 D 6.33
          10 D 6.33


          Alternative from markus



          idx <- with(df, ave(Value, Class, FUN = function(x) length(unique(x))) == 1)
          df[idx, ]





          share|improve this answer





















          • 3





            I was about to post a similar solution using ave which you might want to add as alternative. idx <- with(df, ave(Value, Class, FUN = function(x) length(unique(x))) == 1); df[idx, ]

            – markus
            Jan 18 at 20:22






          • 1





            @markus that is great thank you :-) , if you would like convert it as an answer I will definitively upvote for you man

            – W-B
            Jan 18 at 20:24






          • 2





            That is kind of you. I found it too similiar to what you posted 1 minute earlier. Feel free to use it.

            – markus
            Jan 18 at 20:27





















          0














          With tidyverse you can do:



          df %>%
          group_by(Class) %>%
          filter(all(Value == first(Value)))


          Or:



          df %>%
          group_by(Class) %>%
          filter(n_distinct(Value) == 1)


          Or:



          df %>%
          group_by(Class) %>%
          filter(all(Value %/% first(Value) != 0))


          Or:



          df %>%
          group_by(Class, Value) %>%
          mutate(temp = seq_along(Value)) %>%
          group_by(Class) %>%
          filter(sum(temp[temp == 1]) == 1) %>%
          select(-temp)


          Or basically the same as the post from @W-B:



          df %>%
          group_by(Class) %>%
          filter(length(unique(Value)) == 1)





          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/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%2f54260788%2fhow-to-find-a-column-value-based-on-the-unique-value-for-another-column%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









            3














            Using aggregate with number of unique (length(unique))



            filterdf=aggregate(Value ~ Class, df, function(x) length(unique(x)))
            df[df$Class%in%filterdf[filterdf$Value==1,]$Class,]
            Class Value
            1 A 5.40
            2 A 5.40
            3 A 5.40
            6 C 4.02
            7 C 4.02
            8 C 4.02
            9 D 6.33
            10 D 6.33


            Alternative from markus



            idx <- with(df, ave(Value, Class, FUN = function(x) length(unique(x))) == 1)
            df[idx, ]





            share|improve this answer





















            • 3





              I was about to post a similar solution using ave which you might want to add as alternative. idx <- with(df, ave(Value, Class, FUN = function(x) length(unique(x))) == 1); df[idx, ]

              – markus
              Jan 18 at 20:22






            • 1





              @markus that is great thank you :-) , if you would like convert it as an answer I will definitively upvote for you man

              – W-B
              Jan 18 at 20:24






            • 2





              That is kind of you. I found it too similiar to what you posted 1 minute earlier. Feel free to use it.

              – markus
              Jan 18 at 20:27


















            3














            Using aggregate with number of unique (length(unique))



            filterdf=aggregate(Value ~ Class, df, function(x) length(unique(x)))
            df[df$Class%in%filterdf[filterdf$Value==1,]$Class,]
            Class Value
            1 A 5.40
            2 A 5.40
            3 A 5.40
            6 C 4.02
            7 C 4.02
            8 C 4.02
            9 D 6.33
            10 D 6.33


            Alternative from markus



            idx <- with(df, ave(Value, Class, FUN = function(x) length(unique(x))) == 1)
            df[idx, ]





            share|improve this answer





















            • 3





              I was about to post a similar solution using ave which you might want to add as alternative. idx <- with(df, ave(Value, Class, FUN = function(x) length(unique(x))) == 1); df[idx, ]

              – markus
              Jan 18 at 20:22






            • 1





              @markus that is great thank you :-) , if you would like convert it as an answer I will definitively upvote for you man

              – W-B
              Jan 18 at 20:24






            • 2





              That is kind of you. I found it too similiar to what you posted 1 minute earlier. Feel free to use it.

              – markus
              Jan 18 at 20:27
















            3












            3








            3







            Using aggregate with number of unique (length(unique))



            filterdf=aggregate(Value ~ Class, df, function(x) length(unique(x)))
            df[df$Class%in%filterdf[filterdf$Value==1,]$Class,]
            Class Value
            1 A 5.40
            2 A 5.40
            3 A 5.40
            6 C 4.02
            7 C 4.02
            8 C 4.02
            9 D 6.33
            10 D 6.33


            Alternative from markus



            idx <- with(df, ave(Value, Class, FUN = function(x) length(unique(x))) == 1)
            df[idx, ]





            share|improve this answer















            Using aggregate with number of unique (length(unique))



            filterdf=aggregate(Value ~ Class, df, function(x) length(unique(x)))
            df[df$Class%in%filterdf[filterdf$Value==1,]$Class,]
            Class Value
            1 A 5.40
            2 A 5.40
            3 A 5.40
            6 C 4.02
            7 C 4.02
            8 C 4.02
            9 D 6.33
            10 D 6.33


            Alternative from markus



            idx <- with(df, ave(Value, Class, FUN = function(x) length(unique(x))) == 1)
            df[idx, ]






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Jan 18 at 20:29

























            answered Jan 18 at 20:17









            W-BW-B

            107k83165




            107k83165








            • 3





              I was about to post a similar solution using ave which you might want to add as alternative. idx <- with(df, ave(Value, Class, FUN = function(x) length(unique(x))) == 1); df[idx, ]

              – markus
              Jan 18 at 20:22






            • 1





              @markus that is great thank you :-) , if you would like convert it as an answer I will definitively upvote for you man

              – W-B
              Jan 18 at 20:24






            • 2





              That is kind of you. I found it too similiar to what you posted 1 minute earlier. Feel free to use it.

              – markus
              Jan 18 at 20:27
















            • 3





              I was about to post a similar solution using ave which you might want to add as alternative. idx <- with(df, ave(Value, Class, FUN = function(x) length(unique(x))) == 1); df[idx, ]

              – markus
              Jan 18 at 20:22






            • 1





              @markus that is great thank you :-) , if you would like convert it as an answer I will definitively upvote for you man

              – W-B
              Jan 18 at 20:24






            • 2





              That is kind of you. I found it too similiar to what you posted 1 minute earlier. Feel free to use it.

              – markus
              Jan 18 at 20:27










            3




            3





            I was about to post a similar solution using ave which you might want to add as alternative. idx <- with(df, ave(Value, Class, FUN = function(x) length(unique(x))) == 1); df[idx, ]

            – markus
            Jan 18 at 20:22





            I was about to post a similar solution using ave which you might want to add as alternative. idx <- with(df, ave(Value, Class, FUN = function(x) length(unique(x))) == 1); df[idx, ]

            – markus
            Jan 18 at 20:22




            1




            1





            @markus that is great thank you :-) , if you would like convert it as an answer I will definitively upvote for you man

            – W-B
            Jan 18 at 20:24





            @markus that is great thank you :-) , if you would like convert it as an answer I will definitively upvote for you man

            – W-B
            Jan 18 at 20:24




            2




            2





            That is kind of you. I found it too similiar to what you posted 1 minute earlier. Feel free to use it.

            – markus
            Jan 18 at 20:27







            That is kind of you. I found it too similiar to what you posted 1 minute earlier. Feel free to use it.

            – markus
            Jan 18 at 20:27















            0














            With tidyverse you can do:



            df %>%
            group_by(Class) %>%
            filter(all(Value == first(Value)))


            Or:



            df %>%
            group_by(Class) %>%
            filter(n_distinct(Value) == 1)


            Or:



            df %>%
            group_by(Class) %>%
            filter(all(Value %/% first(Value) != 0))


            Or:



            df %>%
            group_by(Class, Value) %>%
            mutate(temp = seq_along(Value)) %>%
            group_by(Class) %>%
            filter(sum(temp[temp == 1]) == 1) %>%
            select(-temp)


            Or basically the same as the post from @W-B:



            df %>%
            group_by(Class) %>%
            filter(length(unique(Value)) == 1)





            share|improve this answer






























              0














              With tidyverse you can do:



              df %>%
              group_by(Class) %>%
              filter(all(Value == first(Value)))


              Or:



              df %>%
              group_by(Class) %>%
              filter(n_distinct(Value) == 1)


              Or:



              df %>%
              group_by(Class) %>%
              filter(all(Value %/% first(Value) != 0))


              Or:



              df %>%
              group_by(Class, Value) %>%
              mutate(temp = seq_along(Value)) %>%
              group_by(Class) %>%
              filter(sum(temp[temp == 1]) == 1) %>%
              select(-temp)


              Or basically the same as the post from @W-B:



              df %>%
              group_by(Class) %>%
              filter(length(unique(Value)) == 1)





              share|improve this answer




























                0












                0








                0







                With tidyverse you can do:



                df %>%
                group_by(Class) %>%
                filter(all(Value == first(Value)))


                Or:



                df %>%
                group_by(Class) %>%
                filter(n_distinct(Value) == 1)


                Or:



                df %>%
                group_by(Class) %>%
                filter(all(Value %/% first(Value) != 0))


                Or:



                df %>%
                group_by(Class, Value) %>%
                mutate(temp = seq_along(Value)) %>%
                group_by(Class) %>%
                filter(sum(temp[temp == 1]) == 1) %>%
                select(-temp)


                Or basically the same as the post from @W-B:



                df %>%
                group_by(Class) %>%
                filter(length(unique(Value)) == 1)





                share|improve this answer















                With tidyverse you can do:



                df %>%
                group_by(Class) %>%
                filter(all(Value == first(Value)))


                Or:



                df %>%
                group_by(Class) %>%
                filter(n_distinct(Value) == 1)


                Or:



                df %>%
                group_by(Class) %>%
                filter(all(Value %/% first(Value) != 0))


                Or:



                df %>%
                group_by(Class, Value) %>%
                mutate(temp = seq_along(Value)) %>%
                group_by(Class) %>%
                filter(sum(temp[temp == 1]) == 1) %>%
                select(-temp)


                Or basically the same as the post from @W-B:



                df %>%
                group_by(Class) %>%
                filter(length(unique(Value)) == 1)






                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Jan 18 at 21:25

























                answered Jan 18 at 21:00









                tmfmnktmfmnk

                2,2941412




                2,2941412






























                    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%2f54260788%2fhow-to-find-a-column-value-based-on-the-unique-value-for-another-column%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

                    Liquibase includeAll doesn't find base path

                    How to use setInterval in EJS file?

                    Petrus Granier-Deferre