Renaming columns according to vector inside pipe











up vote
1
down vote

favorite












I have a data.frame df with columns A and B:



df <- data.frame(A = 1:5, B = 11:15)


There's another data.frame, df2, which I'm building by various calculations that ends up having generic column names X1 and X2, which I cannot control directly (because it passes through being a matrix at one point). So it ends up being something like:



mtrx <- matrix(1:10, ncol = 2)
mtrx %>% data.frame()


I would like to rename the columns in df2 to be the same as df. I could, of course, do it after I finish building df2 with a simple assigning:



names(df2)<-names(df)


My question is - is there a way to do this directly within the pipe? I can't seem to use dplyr::rename, because these have to be in the form of newname=oldname, and I can't seem to vectorize it. Same goes to the data.frame call itself - I can't just give it a vector of column names, as far as I can tell. Is there another option I'm missing? What I'm hoping for is something like



mtrx %>% data.frame() %>% rename(names(df))


but this doesn't work - gives error Error: All arguments must be named.



Cheers!










share|improve this question




























    up vote
    1
    down vote

    favorite












    I have a data.frame df with columns A and B:



    df <- data.frame(A = 1:5, B = 11:15)


    There's another data.frame, df2, which I'm building by various calculations that ends up having generic column names X1 and X2, which I cannot control directly (because it passes through being a matrix at one point). So it ends up being something like:



    mtrx <- matrix(1:10, ncol = 2)
    mtrx %>% data.frame()


    I would like to rename the columns in df2 to be the same as df. I could, of course, do it after I finish building df2 with a simple assigning:



    names(df2)<-names(df)


    My question is - is there a way to do this directly within the pipe? I can't seem to use dplyr::rename, because these have to be in the form of newname=oldname, and I can't seem to vectorize it. Same goes to the data.frame call itself - I can't just give it a vector of column names, as far as I can tell. Is there another option I'm missing? What I'm hoping for is something like



    mtrx %>% data.frame() %>% rename(names(df))


    but this doesn't work - gives error Error: All arguments must be named.



    Cheers!










    share|improve this question


























      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      I have a data.frame df with columns A and B:



      df <- data.frame(A = 1:5, B = 11:15)


      There's another data.frame, df2, which I'm building by various calculations that ends up having generic column names X1 and X2, which I cannot control directly (because it passes through being a matrix at one point). So it ends up being something like:



      mtrx <- matrix(1:10, ncol = 2)
      mtrx %>% data.frame()


      I would like to rename the columns in df2 to be the same as df. I could, of course, do it after I finish building df2 with a simple assigning:



      names(df2)<-names(df)


      My question is - is there a way to do this directly within the pipe? I can't seem to use dplyr::rename, because these have to be in the form of newname=oldname, and I can't seem to vectorize it. Same goes to the data.frame call itself - I can't just give it a vector of column names, as far as I can tell. Is there another option I'm missing? What I'm hoping for is something like



      mtrx %>% data.frame() %>% rename(names(df))


      but this doesn't work - gives error Error: All arguments must be named.



      Cheers!










      share|improve this question















      I have a data.frame df with columns A and B:



      df <- data.frame(A = 1:5, B = 11:15)


      There's another data.frame, df2, which I'm building by various calculations that ends up having generic column names X1 and X2, which I cannot control directly (because it passes through being a matrix at one point). So it ends up being something like:



      mtrx <- matrix(1:10, ncol = 2)
      mtrx %>% data.frame()


      I would like to rename the columns in df2 to be the same as df. I could, of course, do it after I finish building df2 with a simple assigning:



      names(df2)<-names(df)


      My question is - is there a way to do this directly within the pipe? I can't seem to use dplyr::rename, because these have to be in the form of newname=oldname, and I can't seem to vectorize it. Same goes to the data.frame call itself - I can't just give it a vector of column names, as far as I can tell. Is there another option I'm missing? What I'm hoping for is something like



      mtrx %>% data.frame() %>% rename(names(df))


      but this doesn't work - gives error Error: All arguments must be named.



      Cheers!







      r pipe






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 18 at 7:38









      markus

      8,440928




      8,440928










      asked Nov 18 at 6:42









      iod

      2,9671619




      2,9671619
























          2 Answers
          2






          active

          oldest

          votes

















          up vote
          1
          down vote



          accepted










          We can use rename_all from tidyverse



          library(tidyverse)
          mtrx %>%
          as.data.frame %>%
          rename_all(~ names(df))
          # A B
          # 1 1 6
          # 2 2 7
          # 3 3 8
          # 4 4 9
          # 5 5 10





          share|improve this answer

















          • 1




            That's what I was looking for! Thanks
            – iod
            Nov 18 at 21:15


















          up vote
          2
          down vote













          You can use setNames



          mtrx %>% 
          data.frame() %>%
          setNames(., nm = names(df))
          # A B
          #1 1 6
          #2 2 7
          #3 3 8
          #4 4 9
          #5 5 10


          Or use purrr's equivalent set_names



          mtrx %>% 
          data.frame() %>%
          purrr::set_names(., nm = names(df))




          A third option is "names<-"



          mtrx %>% 
          data.frame() %>%
          "names<-"(names(df))





          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',
            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%2f53358516%2frenaming-columns-according-to-vector-inside-pipe%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








            up vote
            1
            down vote



            accepted










            We can use rename_all from tidyverse



            library(tidyverse)
            mtrx %>%
            as.data.frame %>%
            rename_all(~ names(df))
            # A B
            # 1 1 6
            # 2 2 7
            # 3 3 8
            # 4 4 9
            # 5 5 10





            share|improve this answer

















            • 1




              That's what I was looking for! Thanks
              – iod
              Nov 18 at 21:15















            up vote
            1
            down vote



            accepted










            We can use rename_all from tidyverse



            library(tidyverse)
            mtrx %>%
            as.data.frame %>%
            rename_all(~ names(df))
            # A B
            # 1 1 6
            # 2 2 7
            # 3 3 8
            # 4 4 9
            # 5 5 10





            share|improve this answer

















            • 1




              That's what I was looking for! Thanks
              – iod
              Nov 18 at 21:15













            up vote
            1
            down vote



            accepted







            up vote
            1
            down vote



            accepted






            We can use rename_all from tidyverse



            library(tidyverse)
            mtrx %>%
            as.data.frame %>%
            rename_all(~ names(df))
            # A B
            # 1 1 6
            # 2 2 7
            # 3 3 8
            # 4 4 9
            # 5 5 10





            share|improve this answer












            We can use rename_all from tidyverse



            library(tidyverse)
            mtrx %>%
            as.data.frame %>%
            rename_all(~ names(df))
            # A B
            # 1 1 6
            # 2 2 7
            # 3 3 8
            # 4 4 9
            # 5 5 10






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 18 at 20:55









            akrun

            390k13178251




            390k13178251








            • 1




              That's what I was looking for! Thanks
              – iod
              Nov 18 at 21:15














            • 1




              That's what I was looking for! Thanks
              – iod
              Nov 18 at 21:15








            1




            1




            That's what I was looking for! Thanks
            – iod
            Nov 18 at 21:15




            That's what I was looking for! Thanks
            – iod
            Nov 18 at 21:15












            up vote
            2
            down vote













            You can use setNames



            mtrx %>% 
            data.frame() %>%
            setNames(., nm = names(df))
            # A B
            #1 1 6
            #2 2 7
            #3 3 8
            #4 4 9
            #5 5 10


            Or use purrr's equivalent set_names



            mtrx %>% 
            data.frame() %>%
            purrr::set_names(., nm = names(df))




            A third option is "names<-"



            mtrx %>% 
            data.frame() %>%
            "names<-"(names(df))





            share|improve this answer



























              up vote
              2
              down vote













              You can use setNames



              mtrx %>% 
              data.frame() %>%
              setNames(., nm = names(df))
              # A B
              #1 1 6
              #2 2 7
              #3 3 8
              #4 4 9
              #5 5 10


              Or use purrr's equivalent set_names



              mtrx %>% 
              data.frame() %>%
              purrr::set_names(., nm = names(df))




              A third option is "names<-"



              mtrx %>% 
              data.frame() %>%
              "names<-"(names(df))





              share|improve this answer

























                up vote
                2
                down vote










                up vote
                2
                down vote









                You can use setNames



                mtrx %>% 
                data.frame() %>%
                setNames(., nm = names(df))
                # A B
                #1 1 6
                #2 2 7
                #3 3 8
                #4 4 9
                #5 5 10


                Or use purrr's equivalent set_names



                mtrx %>% 
                data.frame() %>%
                purrr::set_names(., nm = names(df))




                A third option is "names<-"



                mtrx %>% 
                data.frame() %>%
                "names<-"(names(df))





                share|improve this answer














                You can use setNames



                mtrx %>% 
                data.frame() %>%
                setNames(., nm = names(df))
                # A B
                #1 1 6
                #2 2 7
                #3 3 8
                #4 4 9
                #5 5 10


                Or use purrr's equivalent set_names



                mtrx %>% 
                data.frame() %>%
                purrr::set_names(., nm = names(df))




                A third option is "names<-"



                mtrx %>% 
                data.frame() %>%
                "names<-"(names(df))






                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Nov 18 at 7:44

























                answered Nov 18 at 7:37









                markus

                8,440928




                8,440928






























                     

                    draft saved


                    draft discarded



















































                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53358516%2frenaming-columns-according-to-vector-inside-pipe%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

                    Ottavio Pratesi

                    Tricia Helfer

                    15 giugno