How to drop observations based on conditions










1















I have a subset data that has a total count for each observation from a bigger dataset. If I want to drop duplicates based on a higher count and drop codes that appear less if the name is the same, how would I go about that? So for instance:



name = c("a", "a", "b", "b", "b", "c", "d", "e", "e", "e")
code = c(1,1,2,3,4,1,1,2,2,3)
n = c(1,10,2,3,5,4,8,100,90,40)
data = data.frame(name,code,n)


The end product would be left with these:



name = c("a", "b", "c", "d", "e")
code = c(1,4,1,1,2)
n = c(10,5,4,8,100)
data2 = data.frame(name,code,n)









share|improve this question



















  • 1





    Side note: do not do data.frame(cbind(...)). You've turned all your numeric variables into characters. The function data.frame() is all you need: data.frame(name,code,n).

    – joran
    Nov 14 '18 at 19:34






  • 1





    @joran Thank you. will change that now

    – Sun
    Nov 14 '18 at 19:42






  • 2





    Possible duplicate of Remove duplicates keeping entry with largest absolute value

    – Daniel Fischer
    Nov 14 '18 at 19:56















1















I have a subset data that has a total count for each observation from a bigger dataset. If I want to drop duplicates based on a higher count and drop codes that appear less if the name is the same, how would I go about that? So for instance:



name = c("a", "a", "b", "b", "b", "c", "d", "e", "e", "e")
code = c(1,1,2,3,4,1,1,2,2,3)
n = c(1,10,2,3,5,4,8,100,90,40)
data = data.frame(name,code,n)


The end product would be left with these:



name = c("a", "b", "c", "d", "e")
code = c(1,4,1,1,2)
n = c(10,5,4,8,100)
data2 = data.frame(name,code,n)









share|improve this question



















  • 1





    Side note: do not do data.frame(cbind(...)). You've turned all your numeric variables into characters. The function data.frame() is all you need: data.frame(name,code,n).

    – joran
    Nov 14 '18 at 19:34






  • 1





    @joran Thank you. will change that now

    – Sun
    Nov 14 '18 at 19:42






  • 2





    Possible duplicate of Remove duplicates keeping entry with largest absolute value

    – Daniel Fischer
    Nov 14 '18 at 19:56













1












1








1








I have a subset data that has a total count for each observation from a bigger dataset. If I want to drop duplicates based on a higher count and drop codes that appear less if the name is the same, how would I go about that? So for instance:



name = c("a", "a", "b", "b", "b", "c", "d", "e", "e", "e")
code = c(1,1,2,3,4,1,1,2,2,3)
n = c(1,10,2,3,5,4,8,100,90,40)
data = data.frame(name,code,n)


The end product would be left with these:



name = c("a", "b", "c", "d", "e")
code = c(1,4,1,1,2)
n = c(10,5,4,8,100)
data2 = data.frame(name,code,n)









share|improve this question
















I have a subset data that has a total count for each observation from a bigger dataset. If I want to drop duplicates based on a higher count and drop codes that appear less if the name is the same, how would I go about that? So for instance:



name = c("a", "a", "b", "b", "b", "c", "d", "e", "e", "e")
code = c(1,1,2,3,4,1,1,2,2,3)
n = c(1,10,2,3,5,4,8,100,90,40)
data = data.frame(name,code,n)


The end product would be left with these:



name = c("a", "b", "c", "d", "e")
code = c(1,4,1,1,2)
n = c(10,5,4,8,100)
data2 = data.frame(name,code,n)






r duplicates






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 14 '18 at 19:42







Sun

















asked Nov 14 '18 at 19:32









SunSun

596




596







  • 1





    Side note: do not do data.frame(cbind(...)). You've turned all your numeric variables into characters. The function data.frame() is all you need: data.frame(name,code,n).

    – joran
    Nov 14 '18 at 19:34






  • 1





    @joran Thank you. will change that now

    – Sun
    Nov 14 '18 at 19:42






  • 2





    Possible duplicate of Remove duplicates keeping entry with largest absolute value

    – Daniel Fischer
    Nov 14 '18 at 19:56












  • 1





    Side note: do not do data.frame(cbind(...)). You've turned all your numeric variables into characters. The function data.frame() is all you need: data.frame(name,code,n).

    – joran
    Nov 14 '18 at 19:34






  • 1





    @joran Thank you. will change that now

    – Sun
    Nov 14 '18 at 19:42






  • 2





    Possible duplicate of Remove duplicates keeping entry with largest absolute value

    – Daniel Fischer
    Nov 14 '18 at 19:56







1




1





Side note: do not do data.frame(cbind(...)). You've turned all your numeric variables into characters. The function data.frame() is all you need: data.frame(name,code,n).

– joran
Nov 14 '18 at 19:34





Side note: do not do data.frame(cbind(...)). You've turned all your numeric variables into characters. The function data.frame() is all you need: data.frame(name,code,n).

– joran
Nov 14 '18 at 19:34




1




1





@joran Thank you. will change that now

– Sun
Nov 14 '18 at 19:42





@joran Thank you. will change that now

– Sun
Nov 14 '18 at 19:42




2




2





Possible duplicate of Remove duplicates keeping entry with largest absolute value

– Daniel Fischer
Nov 14 '18 at 19:56





Possible duplicate of Remove duplicates keeping entry with largest absolute value

– Daniel Fischer
Nov 14 '18 at 19:56












1 Answer
1






active

oldest

votes


















1














If you can use dplyr, this should do the trick:



library(dplyr)
data %>%
group_by(name) %>%
filter(n == max(n)) %>%
ungroup()





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%2f53307554%2fhow-to-drop-observations-based-on-conditions%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    If you can use dplyr, this should do the trick:



    library(dplyr)
    data %>%
    group_by(name) %>%
    filter(n == max(n)) %>%
    ungroup()





    share|improve this answer



























      1














      If you can use dplyr, this should do the trick:



      library(dplyr)
      data %>%
      group_by(name) %>%
      filter(n == max(n)) %>%
      ungroup()





      share|improve this answer

























        1












        1








        1







        If you can use dplyr, this should do the trick:



        library(dplyr)
        data %>%
        group_by(name) %>%
        filter(n == max(n)) %>%
        ungroup()





        share|improve this answer













        If you can use dplyr, this should do the trick:



        library(dplyr)
        data %>%
        group_by(name) %>%
        filter(n == max(n)) %>%
        ungroup()






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 14 '18 at 21:35









        dmcadmca

        443414




        443414





























            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%2f53307554%2fhow-to-drop-observations-based-on-conditions%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

            27

            Top Tejano songwriter Luis Silva dead of heart attack at 64

            Category:Rhetoric