Renaming columns not containing specific suffix












1















I want to rename columns in dataframe 'SOURCE_SUBSET' not contain the substring "_dich" with the suffix '_dich' and all other columns to have the original name.



exmaple:
GGADA202_dich should remain the same
GGADA203 should become GGADA203_dich



This is what I have so far. It is not working correctly: GGADA202_dich becomes GGADA202_dich_dich_dich



ifelse(grepl("_dich", colnames(SOURCE_SUBSET), fixed=TRUE),
colnames(SOURCE_SUBSET) <- paste(colnames(SOURCE_SUBSET), "dich", sep = "_"),colnames(SOURCE_SUBSET))









share|improve this question


















  • 1





    Try colnames(SOURCE_SUBSET) <- sub("^(?!.*_dich)(.*)", "\1_dich", colnames(SOURCE_SUBSET), perl=TRUE)

    – Wiktor Stribiżew
    Nov 23 '18 at 12:24











  • it renames all with the suffix...

    – Mark Henry
    Nov 23 '18 at 12:31











  • The "^(?!.*_dich)(.*)" pattern matches strings that do not contain _dich. Make sure you use perl=TRUE. Those that do not match remain as is. See this demo.

    – Wiktor Stribiżew
    Nov 23 '18 at 12:33








  • 1





    See ideone.com/3PjzjA

    – Wiktor Stribiżew
    Nov 23 '18 at 12:37











  • Thank you Wiktor!

    – Mark Henry
    Nov 23 '18 at 12:43
















1















I want to rename columns in dataframe 'SOURCE_SUBSET' not contain the substring "_dich" with the suffix '_dich' and all other columns to have the original name.



exmaple:
GGADA202_dich should remain the same
GGADA203 should become GGADA203_dich



This is what I have so far. It is not working correctly: GGADA202_dich becomes GGADA202_dich_dich_dich



ifelse(grepl("_dich", colnames(SOURCE_SUBSET), fixed=TRUE),
colnames(SOURCE_SUBSET) <- paste(colnames(SOURCE_SUBSET), "dich", sep = "_"),colnames(SOURCE_SUBSET))









share|improve this question


















  • 1





    Try colnames(SOURCE_SUBSET) <- sub("^(?!.*_dich)(.*)", "\1_dich", colnames(SOURCE_SUBSET), perl=TRUE)

    – Wiktor Stribiżew
    Nov 23 '18 at 12:24











  • it renames all with the suffix...

    – Mark Henry
    Nov 23 '18 at 12:31











  • The "^(?!.*_dich)(.*)" pattern matches strings that do not contain _dich. Make sure you use perl=TRUE. Those that do not match remain as is. See this demo.

    – Wiktor Stribiżew
    Nov 23 '18 at 12:33








  • 1





    See ideone.com/3PjzjA

    – Wiktor Stribiżew
    Nov 23 '18 at 12:37











  • Thank you Wiktor!

    – Mark Henry
    Nov 23 '18 at 12:43














1












1








1








I want to rename columns in dataframe 'SOURCE_SUBSET' not contain the substring "_dich" with the suffix '_dich' and all other columns to have the original name.



exmaple:
GGADA202_dich should remain the same
GGADA203 should become GGADA203_dich



This is what I have so far. It is not working correctly: GGADA202_dich becomes GGADA202_dich_dich_dich



ifelse(grepl("_dich", colnames(SOURCE_SUBSET), fixed=TRUE),
colnames(SOURCE_SUBSET) <- paste(colnames(SOURCE_SUBSET), "dich", sep = "_"),colnames(SOURCE_SUBSET))









share|improve this question














I want to rename columns in dataframe 'SOURCE_SUBSET' not contain the substring "_dich" with the suffix '_dich' and all other columns to have the original name.



exmaple:
GGADA202_dich should remain the same
GGADA203 should become GGADA203_dich



This is what I have so far. It is not working correctly: GGADA202_dich becomes GGADA202_dich_dich_dich



ifelse(grepl("_dich", colnames(SOURCE_SUBSET), fixed=TRUE),
colnames(SOURCE_SUBSET) <- paste(colnames(SOURCE_SUBSET), "dich", sep = "_"),colnames(SOURCE_SUBSET))






r grepl






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 23 '18 at 12:21









Mark HenryMark Henry

1,12952947




1,12952947








  • 1





    Try colnames(SOURCE_SUBSET) <- sub("^(?!.*_dich)(.*)", "\1_dich", colnames(SOURCE_SUBSET), perl=TRUE)

    – Wiktor Stribiżew
    Nov 23 '18 at 12:24











  • it renames all with the suffix...

    – Mark Henry
    Nov 23 '18 at 12:31











  • The "^(?!.*_dich)(.*)" pattern matches strings that do not contain _dich. Make sure you use perl=TRUE. Those that do not match remain as is. See this demo.

    – Wiktor Stribiżew
    Nov 23 '18 at 12:33








  • 1





    See ideone.com/3PjzjA

    – Wiktor Stribiżew
    Nov 23 '18 at 12:37











  • Thank you Wiktor!

    – Mark Henry
    Nov 23 '18 at 12:43














  • 1





    Try colnames(SOURCE_SUBSET) <- sub("^(?!.*_dich)(.*)", "\1_dich", colnames(SOURCE_SUBSET), perl=TRUE)

    – Wiktor Stribiżew
    Nov 23 '18 at 12:24











  • it renames all with the suffix...

    – Mark Henry
    Nov 23 '18 at 12:31











  • The "^(?!.*_dich)(.*)" pattern matches strings that do not contain _dich. Make sure you use perl=TRUE. Those that do not match remain as is. See this demo.

    – Wiktor Stribiżew
    Nov 23 '18 at 12:33








  • 1





    See ideone.com/3PjzjA

    – Wiktor Stribiżew
    Nov 23 '18 at 12:37











  • Thank you Wiktor!

    – Mark Henry
    Nov 23 '18 at 12:43








1




1





Try colnames(SOURCE_SUBSET) <- sub("^(?!.*_dich)(.*)", "\1_dich", colnames(SOURCE_SUBSET), perl=TRUE)

– Wiktor Stribiżew
Nov 23 '18 at 12:24





Try colnames(SOURCE_SUBSET) <- sub("^(?!.*_dich)(.*)", "\1_dich", colnames(SOURCE_SUBSET), perl=TRUE)

– Wiktor Stribiżew
Nov 23 '18 at 12:24













it renames all with the suffix...

– Mark Henry
Nov 23 '18 at 12:31





it renames all with the suffix...

– Mark Henry
Nov 23 '18 at 12:31













The "^(?!.*_dich)(.*)" pattern matches strings that do not contain _dich. Make sure you use perl=TRUE. Those that do not match remain as is. See this demo.

– Wiktor Stribiżew
Nov 23 '18 at 12:33







The "^(?!.*_dich)(.*)" pattern matches strings that do not contain _dich. Make sure you use perl=TRUE. Those that do not match remain as is. See this demo.

– Wiktor Stribiżew
Nov 23 '18 at 12:33






1




1





See ideone.com/3PjzjA

– Wiktor Stribiżew
Nov 23 '18 at 12:37





See ideone.com/3PjzjA

– Wiktor Stribiżew
Nov 23 '18 at 12:37













Thank you Wiktor!

– Mark Henry
Nov 23 '18 at 12:43





Thank you Wiktor!

– Mark Henry
Nov 23 '18 at 12:43












2 Answers
2






active

oldest

votes


















2














You may use



colnames(SOURCE_SUBSET) <- sub("^(?!.*_dich)(.*)", "\1_dich", colnames(SOURCE_SUBSET), perl=TRUE)


See the R demo online and the regex demo.



The ^(?!.*_dich)(.*) pattern matches strings that do not contain _dich. Make sure you use perl=TRUE as lookaheads are not supported by the default TRE library. Those names that do not match will remain as is.



Pattern details





  • ^ - start of string


  • (?!.*_dich) - no _dich is allowed after any 0+ chars other than line break chars as many as possible from the start of the string


  • (.*) - grabs into Group 1 (this text is inserted back into the result using '\1' in the replacement pattern) any 0+ chars other than line break chars as many as possible.






share|improve this answer































    1














    tidyverse approach



    library( tidyverse )
    #if a column name does not contain the string"_dich", add the suffix "_dich"
    df %>% rename_at( vars( -contains( "_dich" ) ), funs( paste0(., "_dich") ) )





    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%2f53446654%2frenaming-columns-not-containing-specific-suffix%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









      2














      You may use



      colnames(SOURCE_SUBSET) <- sub("^(?!.*_dich)(.*)", "\1_dich", colnames(SOURCE_SUBSET), perl=TRUE)


      See the R demo online and the regex demo.



      The ^(?!.*_dich)(.*) pattern matches strings that do not contain _dich. Make sure you use perl=TRUE as lookaheads are not supported by the default TRE library. Those names that do not match will remain as is.



      Pattern details





      • ^ - start of string


      • (?!.*_dich) - no _dich is allowed after any 0+ chars other than line break chars as many as possible from the start of the string


      • (.*) - grabs into Group 1 (this text is inserted back into the result using '\1' in the replacement pattern) any 0+ chars other than line break chars as many as possible.






      share|improve this answer




























        2














        You may use



        colnames(SOURCE_SUBSET) <- sub("^(?!.*_dich)(.*)", "\1_dich", colnames(SOURCE_SUBSET), perl=TRUE)


        See the R demo online and the regex demo.



        The ^(?!.*_dich)(.*) pattern matches strings that do not contain _dich. Make sure you use perl=TRUE as lookaheads are not supported by the default TRE library. Those names that do not match will remain as is.



        Pattern details





        • ^ - start of string


        • (?!.*_dich) - no _dich is allowed after any 0+ chars other than line break chars as many as possible from the start of the string


        • (.*) - grabs into Group 1 (this text is inserted back into the result using '\1' in the replacement pattern) any 0+ chars other than line break chars as many as possible.






        share|improve this answer


























          2












          2








          2







          You may use



          colnames(SOURCE_SUBSET) <- sub("^(?!.*_dich)(.*)", "\1_dich", colnames(SOURCE_SUBSET), perl=TRUE)


          See the R demo online and the regex demo.



          The ^(?!.*_dich)(.*) pattern matches strings that do not contain _dich. Make sure you use perl=TRUE as lookaheads are not supported by the default TRE library. Those names that do not match will remain as is.



          Pattern details





          • ^ - start of string


          • (?!.*_dich) - no _dich is allowed after any 0+ chars other than line break chars as many as possible from the start of the string


          • (.*) - grabs into Group 1 (this text is inserted back into the result using '\1' in the replacement pattern) any 0+ chars other than line break chars as many as possible.






          share|improve this answer













          You may use



          colnames(SOURCE_SUBSET) <- sub("^(?!.*_dich)(.*)", "\1_dich", colnames(SOURCE_SUBSET), perl=TRUE)


          See the R demo online and the regex demo.



          The ^(?!.*_dich)(.*) pattern matches strings that do not contain _dich. Make sure you use perl=TRUE as lookaheads are not supported by the default TRE library. Those names that do not match will remain as is.



          Pattern details





          • ^ - start of string


          • (?!.*_dich) - no _dich is allowed after any 0+ chars other than line break chars as many as possible from the start of the string


          • (.*) - grabs into Group 1 (this text is inserted back into the result using '\1' in the replacement pattern) any 0+ chars other than line break chars as many as possible.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 23 '18 at 12:45









          Wiktor StribiżewWiktor Stribiżew

          328k16147227




          328k16147227

























              1














              tidyverse approach



              library( tidyverse )
              #if a column name does not contain the string"_dich", add the suffix "_dich"
              df %>% rename_at( vars( -contains( "_dich" ) ), funs( paste0(., "_dich") ) )





              share|improve this answer




























                1














                tidyverse approach



                library( tidyverse )
                #if a column name does not contain the string"_dich", add the suffix "_dich"
                df %>% rename_at( vars( -contains( "_dich" ) ), funs( paste0(., "_dich") ) )





                share|improve this answer


























                  1












                  1








                  1







                  tidyverse approach



                  library( tidyverse )
                  #if a column name does not contain the string"_dich", add the suffix "_dich"
                  df %>% rename_at( vars( -contains( "_dich" ) ), funs( paste0(., "_dich") ) )





                  share|improve this answer













                  tidyverse approach



                  library( tidyverse )
                  #if a column name does not contain the string"_dich", add the suffix "_dich"
                  df %>% rename_at( vars( -contains( "_dich" ) ), funs( paste0(., "_dich") ) )






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 23 '18 at 14:15









                  WimpelWimpel

                  6,302323




                  6,302323






























                      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%2f53446654%2frenaming-columns-not-containing-specific-suffix%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







                      這個網誌中的熱門文章

                      Academy of Television Arts & Sciences

                      L'Équipe

                      1995 France bombings