Select columns based on multiple conditions on column namesHow to make a great R reproducible exampleHow to sort a dataframe by multiple column(s)Drop data frame columns by nameChanging column names of a data frameFlexible subseting data.table in Rconditional random matching from one data frame into another data frameDouble match in rHow to produce a binary column in R from an existing numerical column?Summing in R with multiple conditionsCreating new column based on row values of multiple data subsetting conditionsCounting the rows in a data frame that satisfy some criteria and grouping them by the unique values in the first column of the data frame

Digital Bananas

Could the Queen overturn the UK Supreme Court ruling regarding prorogation of Parliament?

The answer is a girl's name (my future granddaughter) - can anyone help?

Do jackscrews suffer from blowdown?

Looking for circuit board material that can be dissolved

Using RECURSIVE in Virtual Layer

Did Joe Biden "stop a prosecution" into his son in Ukraine? And did he brag about stopping the prosecution?

Avoiding dust scattering when you drill

How is this situation not a checkmate?

What did the Federation give the Prophets in exchange for access to the wormhole in DS9?

Is "weekend warrior" derogatory?

Notation clarity question for a conglomerate of accidentals

Is spot metering just an EV compensation?

What is the point of impeaching Trump?

Why the first octet of a MAC address always end with a binary 0?

French license plates

Re-entering the UK after overstaying in 2008

PhD Length: are shorter PhD degrees (from different countries) valued differently in other counter countries where PhD Is a longer process?

The difference of Prime in Solve doesn't work

What is the difference between increasing volume and increasing gain?

How to level a picture frame hung on a single nail?

How to say "respectively" in German when listing (enumerating) things

What are one's options when facing religious discrimination at the airport?

Did the Soviet army intentionally send troops (e.g. penal battalions) running over minefields?



Select columns based on multiple conditions on column names


How to make a great R reproducible exampleHow to sort a dataframe by multiple column(s)Drop data frame columns by nameChanging column names of a data frameFlexible subseting data.table in Rconditional random matching from one data frame into another data frameDouble match in rHow to produce a binary column in R from an existing numerical column?Summing in R with multiple conditionsCreating new column based on row values of multiple data subsetting conditionsCounting the rows in a data frame that satisfy some criteria and grouping them by the unique values in the first column of the data frame






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty
margin-bottom:0;









0















I'm trying to sum over columns containing certain characters, and i don't know how to achieve it by using grep-.



I want to sum the following columns. they all have "Under 1.30" and "Under 5 years only" in the names:



"Under 1.30: - Married-couple family: - With related children of the householder under 18 years: - Under 5 years only"

"Under 1.30: - Other family: - Male householder, no wife present: - With related children of the householder under 18 years: - Under 5 years only"

"Under 1.30: - Other family: - Female householder, no husband present: - With related children of the householder under 18 years: - Under 5 years only"


I tried the following code, but it returns more columns in addition to the 3 shown above.



names(B17022[,grep("^Under 1.30.[Under 5 years only]", names(B17022))]) 


For instance, it returns too:



"Under 1.30: - Married-couple family:" 









share|improve this question





















  • 1





    Please provide some or all of B17022 in a plain text format to make this question reproducible.

    – neilfws
    Mar 28 at 21:22

















0















I'm trying to sum over columns containing certain characters, and i don't know how to achieve it by using grep-.



I want to sum the following columns. they all have "Under 1.30" and "Under 5 years only" in the names:



"Under 1.30: - Married-couple family: - With related children of the householder under 18 years: - Under 5 years only"

"Under 1.30: - Other family: - Male householder, no wife present: - With related children of the householder under 18 years: - Under 5 years only"

"Under 1.30: - Other family: - Female householder, no husband present: - With related children of the householder under 18 years: - Under 5 years only"


I tried the following code, but it returns more columns in addition to the 3 shown above.



names(B17022[,grep("^Under 1.30.[Under 5 years only]", names(B17022))]) 


For instance, it returns too:



"Under 1.30: - Married-couple family:" 









share|improve this question





















  • 1





    Please provide some or all of B17022 in a plain text format to make this question reproducible.

    – neilfws
    Mar 28 at 21:22













0












0








0








I'm trying to sum over columns containing certain characters, and i don't know how to achieve it by using grep-.



I want to sum the following columns. they all have "Under 1.30" and "Under 5 years only" in the names:



"Under 1.30: - Married-couple family: - With related children of the householder under 18 years: - Under 5 years only"

"Under 1.30: - Other family: - Male householder, no wife present: - With related children of the householder under 18 years: - Under 5 years only"

"Under 1.30: - Other family: - Female householder, no husband present: - With related children of the householder under 18 years: - Under 5 years only"


I tried the following code, but it returns more columns in addition to the 3 shown above.



names(B17022[,grep("^Under 1.30.[Under 5 years only]", names(B17022))]) 


For instance, it returns too:



"Under 1.30: - Married-couple family:" 









share|improve this question
















I'm trying to sum over columns containing certain characters, and i don't know how to achieve it by using grep-.



I want to sum the following columns. they all have "Under 1.30" and "Under 5 years only" in the names:



"Under 1.30: - Married-couple family: - With related children of the householder under 18 years: - Under 5 years only"

"Under 1.30: - Other family: - Male householder, no wife present: - With related children of the householder under 18 years: - Under 5 years only"

"Under 1.30: - Other family: - Female householder, no husband present: - With related children of the householder under 18 years: - Under 5 years only"


I tried the following code, but it returns more columns in addition to the 3 shown above.



names(B17022[,grep("^Under 1.30.[Under 5 years only]", names(B17022))]) 


For instance, it returns too:



"Under 1.30: - Married-couple family:" 






r grep






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 28 at 21:25







Eric Wang

















asked Mar 28 at 21:18









Eric WangEric Wang

181 silver badge8 bronze badges




181 silver badge8 bronze badges










  • 1





    Please provide some or all of B17022 in a plain text format to make this question reproducible.

    – neilfws
    Mar 28 at 21:22












  • 1





    Please provide some or all of B17022 in a plain text format to make this question reproducible.

    – neilfws
    Mar 28 at 21:22







1




1





Please provide some or all of B17022 in a plain text format to make this question reproducible.

– neilfws
Mar 28 at 21:22





Please provide some or all of B17022 in a plain text format to make this question reproducible.

– neilfws
Mar 28 at 21:22












2 Answers
2






active

oldest

votes


















0
















How about:



names(B17022[grep("^Under.*Under 5 years only", names(B17022))])


edit: explanation



The .* matches zero or more of any character except newline. So it will basically match anything in between the two "Under"s.






share|improve this answer



























  • It worked, thank you so much. could you explain what * does here?

    – Eric Wang
    Mar 28 at 21:29


















0
















You could use grepl, which gives you a vector with TRUE or FALSE, what makes it easy to be applied for multiple conditions.



names(B17022)[grepl("Under 1.30", names(B17022)) & 
grepl("Under 5 years only", names(B17022))]


Data used:



B17022 <- data.frame(matrix(rnorm(3), ncol= 3))

names(B17022) <- c("Under 1.30: - Married-couple family: - With related children of the householder under 18 years: - Under 5 years only", "Under 1.30: - Other family: - Male. householder, no wife present: - With related children of the householder under 18 years: - Under 5 years only", "Under 1.30: - Other family: - Female householder, no husband present: - With related children of the householder under 18 years: - Under 5 years only")





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%2f55406984%2fselect-columns-based-on-multiple-conditions-on-column-names%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









    0
















    How about:



    names(B17022[grep("^Under.*Under 5 years only", names(B17022))])


    edit: explanation



    The .* matches zero or more of any character except newline. So it will basically match anything in between the two "Under"s.






    share|improve this answer



























    • It worked, thank you so much. could you explain what * does here?

      – Eric Wang
      Mar 28 at 21:29















    0
















    How about:



    names(B17022[grep("^Under.*Under 5 years only", names(B17022))])


    edit: explanation



    The .* matches zero or more of any character except newline. So it will basically match anything in between the two "Under"s.






    share|improve this answer



























    • It worked, thank you so much. could you explain what * does here?

      – Eric Wang
      Mar 28 at 21:29













    0














    0










    0









    How about:



    names(B17022[grep("^Under.*Under 5 years only", names(B17022))])


    edit: explanation



    The .* matches zero or more of any character except newline. So it will basically match anything in between the two "Under"s.






    share|improve this answer















    How about:



    names(B17022[grep("^Under.*Under 5 years only", names(B17022))])


    edit: explanation



    The .* matches zero or more of any character except newline. So it will basically match anything in between the two "Under"s.







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Mar 28 at 21:35

























    answered Mar 28 at 21:24









    Leo BrueggemanLeo Brueggeman

    3531 silver badge4 bronze badges




    3531 silver badge4 bronze badges















    • It worked, thank you so much. could you explain what * does here?

      – Eric Wang
      Mar 28 at 21:29

















    • It worked, thank you so much. could you explain what * does here?

      – Eric Wang
      Mar 28 at 21:29
















    It worked, thank you so much. could you explain what * does here?

    – Eric Wang
    Mar 28 at 21:29





    It worked, thank you so much. could you explain what * does here?

    – Eric Wang
    Mar 28 at 21:29













    0
















    You could use grepl, which gives you a vector with TRUE or FALSE, what makes it easy to be applied for multiple conditions.



    names(B17022)[grepl("Under 1.30", names(B17022)) & 
    grepl("Under 5 years only", names(B17022))]


    Data used:



    B17022 <- data.frame(matrix(rnorm(3), ncol= 3))

    names(B17022) <- c("Under 1.30: - Married-couple family: - With related children of the householder under 18 years: - Under 5 years only", "Under 1.30: - Other family: - Male. householder, no wife present: - With related children of the householder under 18 years: - Under 5 years only", "Under 1.30: - Other family: - Female householder, no husband present: - With related children of the householder under 18 years: - Under 5 years only")





    share|improve this answer





























      0
















      You could use grepl, which gives you a vector with TRUE or FALSE, what makes it easy to be applied for multiple conditions.



      names(B17022)[grepl("Under 1.30", names(B17022)) & 
      grepl("Under 5 years only", names(B17022))]


      Data used:



      B17022 <- data.frame(matrix(rnorm(3), ncol= 3))

      names(B17022) <- c("Under 1.30: - Married-couple family: - With related children of the householder under 18 years: - Under 5 years only", "Under 1.30: - Other family: - Male. householder, no wife present: - With related children of the householder under 18 years: - Under 5 years only", "Under 1.30: - Other family: - Female householder, no husband present: - With related children of the householder under 18 years: - Under 5 years only")





      share|improve this answer



























        0














        0










        0









        You could use grepl, which gives you a vector with TRUE or FALSE, what makes it easy to be applied for multiple conditions.



        names(B17022)[grepl("Under 1.30", names(B17022)) & 
        grepl("Under 5 years only", names(B17022))]


        Data used:



        B17022 <- data.frame(matrix(rnorm(3), ncol= 3))

        names(B17022) <- c("Under 1.30: - Married-couple family: - With related children of the householder under 18 years: - Under 5 years only", "Under 1.30: - Other family: - Male. householder, no wife present: - With related children of the householder under 18 years: - Under 5 years only", "Under 1.30: - Other family: - Female householder, no husband present: - With related children of the householder under 18 years: - Under 5 years only")





        share|improve this answer













        You could use grepl, which gives you a vector with TRUE or FALSE, what makes it easy to be applied for multiple conditions.



        names(B17022)[grepl("Under 1.30", names(B17022)) & 
        grepl("Under 5 years only", names(B17022))]


        Data used:



        B17022 <- data.frame(matrix(rnorm(3), ncol= 3))

        names(B17022) <- c("Under 1.30: - Married-couple family: - With related children of the householder under 18 years: - Under 5 years only", "Under 1.30: - Other family: - Male. householder, no wife present: - With related children of the householder under 18 years: - Under 5 years only", "Under 1.30: - Other family: - Female householder, no husband present: - With related children of the householder under 18 years: - Under 5 years only")






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Mar 28 at 21:43







        user9992957






































            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%2f55406984%2fselect-columns-based-on-multiple-conditions-on-column-names%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

            Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

            SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

            은진 송씨 목차 역사 본관 분파 인물 조선 왕실과의 인척 관계 집성촌 항렬자 인구 같이 보기 각주 둘러보기 메뉴은진 송씨세종실록 149권, 지리지 충청도 공주목 은진현