Grep multiple string statements

Multi tool use
Multi tool use





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







0















In order to remove a specific string from multiple columns we must use this:



df1 <- with(df, df[ grepl( 'word1', df$Col1) | grepl( 'word1', df$Col2) | grepl( 'word1', df$Col3), ])


If we have more than one string like this:



df1 <- with(df, df[ grepl( 'word1', df$Col1) | grepl( 'word1', df$Col2) | grepl( 'word1', df$Col3), ])
df2 <- with(df, df[ grepl( 'word2', df$Col1) | grepl( 'word2', df$Col2) | grepl( 'word2', df$Col3), ])


How is it possible to have one call instead of many for 'word1; and 'word2' be into one ine?










share|improve this question


















  • 3





    Use grepl( 'word1|word2', df$Col1)

    – MrFlick
    Jan 3 at 21:53








  • 2





    If you have more columns to span, you could try df[rowSums(sapply(df[1:3], grepl, pattern="word1|word2")) > 0,] where [1:3] could be any number of columns including [c("Col1","Col2","Col3")].

    – r2evans
    Jan 3 at 22:10




















0















In order to remove a specific string from multiple columns we must use this:



df1 <- with(df, df[ grepl( 'word1', df$Col1) | grepl( 'word1', df$Col2) | grepl( 'word1', df$Col3), ])


If we have more than one string like this:



df1 <- with(df, df[ grepl( 'word1', df$Col1) | grepl( 'word1', df$Col2) | grepl( 'word1', df$Col3), ])
df2 <- with(df, df[ grepl( 'word2', df$Col1) | grepl( 'word2', df$Col2) | grepl( 'word2', df$Col3), ])


How is it possible to have one call instead of many for 'word1; and 'word2' be into one ine?










share|improve this question


















  • 3





    Use grepl( 'word1|word2', df$Col1)

    – MrFlick
    Jan 3 at 21:53








  • 2





    If you have more columns to span, you could try df[rowSums(sapply(df[1:3], grepl, pattern="word1|word2")) > 0,] where [1:3] could be any number of columns including [c("Col1","Col2","Col3")].

    – r2evans
    Jan 3 at 22:10
















0












0








0








In order to remove a specific string from multiple columns we must use this:



df1 <- with(df, df[ grepl( 'word1', df$Col1) | grepl( 'word1', df$Col2) | grepl( 'word1', df$Col3), ])


If we have more than one string like this:



df1 <- with(df, df[ grepl( 'word1', df$Col1) | grepl( 'word1', df$Col2) | grepl( 'word1', df$Col3), ])
df2 <- with(df, df[ grepl( 'word2', df$Col1) | grepl( 'word2', df$Col2) | grepl( 'word2', df$Col3), ])


How is it possible to have one call instead of many for 'word1; and 'word2' be into one ine?










share|improve this question














In order to remove a specific string from multiple columns we must use this:



df1 <- with(df, df[ grepl( 'word1', df$Col1) | grepl( 'word1', df$Col2) | grepl( 'word1', df$Col3), ])


If we have more than one string like this:



df1 <- with(df, df[ grepl( 'word1', df$Col1) | grepl( 'word1', df$Col2) | grepl( 'word1', df$Col3), ])
df2 <- with(df, df[ grepl( 'word2', df$Col1) | grepl( 'word2', df$Col2) | grepl( 'word2', df$Col3), ])


How is it possible to have one call instead of many for 'word1; and 'word2' be into one ine?







r






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Jan 3 at 21:49









KkyrKkyr

337




337








  • 3





    Use grepl( 'word1|word2', df$Col1)

    – MrFlick
    Jan 3 at 21:53








  • 2





    If you have more columns to span, you could try df[rowSums(sapply(df[1:3], grepl, pattern="word1|word2")) > 0,] where [1:3] could be any number of columns including [c("Col1","Col2","Col3")].

    – r2evans
    Jan 3 at 22:10
















  • 3





    Use grepl( 'word1|word2', df$Col1)

    – MrFlick
    Jan 3 at 21:53








  • 2





    If you have more columns to span, you could try df[rowSums(sapply(df[1:3], grepl, pattern="word1|word2")) > 0,] where [1:3] could be any number of columns including [c("Col1","Col2","Col3")].

    – r2evans
    Jan 3 at 22:10










3




3





Use grepl( 'word1|word2', df$Col1)

– MrFlick
Jan 3 at 21:53







Use grepl( 'word1|word2', df$Col1)

– MrFlick
Jan 3 at 21:53






2




2





If you have more columns to span, you could try df[rowSums(sapply(df[1:3], grepl, pattern="word1|word2")) > 0,] where [1:3] could be any number of columns including [c("Col1","Col2","Col3")].

– r2evans
Jan 3 at 22:10







If you have more columns to span, you could try df[rowSums(sapply(df[1:3], grepl, pattern="word1|word2")) > 0,] where [1:3] could be any number of columns including [c("Col1","Col2","Col3")].

– r2evans
Jan 3 at 22:10














1 Answer
1






active

oldest

votes


















2














First you need the combined regex. You can test it at https://regex101.com/ Then you can use apply() to run it on each column. This will yield a matrix of TRUE or FALSE values. 1 row per variable, 1 column per observation. You can apply() any() on that matrix to get the selection.



test <- apply(df, 2, grepl, pattern = "word1|word2")
df[apply(test, 2, any), ]





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%2f54030284%2fgrep-multiple-string-statements%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









    2














    First you need the combined regex. You can test it at https://regex101.com/ Then you can use apply() to run it on each column. This will yield a matrix of TRUE or FALSE values. 1 row per variable, 1 column per observation. You can apply() any() on that matrix to get the selection.



    test <- apply(df, 2, grepl, pattern = "word1|word2")
    df[apply(test, 2, any), ]





    share|improve this answer




























      2














      First you need the combined regex. You can test it at https://regex101.com/ Then you can use apply() to run it on each column. This will yield a matrix of TRUE or FALSE values. 1 row per variable, 1 column per observation. You can apply() any() on that matrix to get the selection.



      test <- apply(df, 2, grepl, pattern = "word1|word2")
      df[apply(test, 2, any), ]





      share|improve this answer


























        2












        2








        2







        First you need the combined regex. You can test it at https://regex101.com/ Then you can use apply() to run it on each column. This will yield a matrix of TRUE or FALSE values. 1 row per variable, 1 column per observation. You can apply() any() on that matrix to get the selection.



        test <- apply(df, 2, grepl, pattern = "word1|word2")
        df[apply(test, 2, any), ]





        share|improve this answer













        First you need the combined regex. You can test it at https://regex101.com/ Then you can use apply() to run it on each column. This will yield a matrix of TRUE or FALSE values. 1 row per variable, 1 column per observation. You can apply() any() on that matrix to get the selection.



        test <- apply(df, 2, grepl, pattern = "word1|word2")
        df[apply(test, 2, any), ]






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Jan 3 at 22:10









        ThierryThierry

        14.7k43559




        14.7k43559
































            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%2f54030284%2fgrep-multiple-string-statements%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







            L,2FFUeOH
            LUhN,mIxbPmQvlJAshuPhxNEob9MgWZ

            Popular posts from this blog

            Monofisismo

            Angular Downloading a file using contenturl with Basic Authentication

            Olmecas