Renaming columns according to vector inside pipe












1















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





























    1















    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



























      1












      1








      1








      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 '18 at 7:38









      markus

      11.9k1234




      11.9k1234










      asked Nov 18 '18 at 6:42









      iodiod

      3,8332722




      3,8332722
























          2 Answers
          2






          active

          oldest

          votes


















          1














          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 '18 at 21:15



















          2














          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',
            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%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









            1














            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 '18 at 21:15
















            1














            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 '18 at 21:15














            1












            1








            1







            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 '18 at 20:55









            akrunakrun

            406k13196270




            406k13196270








            • 1





              That's what I was looking for! Thanks

              – iod
              Nov 18 '18 at 21:15














            • 1





              That's what I was looking for! Thanks

              – iod
              Nov 18 '18 at 21:15








            1




            1





            That's what I was looking for! Thanks

            – iod
            Nov 18 '18 at 21:15





            That's what I was looking for! Thanks

            – iod
            Nov 18 '18 at 21:15













            2














            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






























              2














              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




























                2












                2








                2







                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 '18 at 7:44

























                answered Nov 18 '18 at 7:37









                markusmarkus

                11.9k1234




                11.9k1234






























                    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%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







                    這個網誌中的熱門文章

                    Tangent Lines Diagram Along Smooth Curve

                    Yusuf al-Mu'taman ibn Hud

                    Zucchini