Error in match.names(clabs, names(xi)) : names do not match previous names












-1
















  1. I am creating a vector having 5 age values and named it as boys_age. Likewise created a vector named girls_age.


eg :



boys_age <- c(18,15,16,17,19)
girls_age<- c(16,14,18,17,15)



  1. Then Append rbind() the two vectors to create data.frame such that I have two columns named group and age.


  2. The values from boys_age and girls_age should be in the column age. The group column should have the category values, boys/girls, to identify the source vector.











share|improve this question





























    -1
















    1. I am creating a vector having 5 age values and named it as boys_age. Likewise created a vector named girls_age.


    eg :



    boys_age <- c(18,15,16,17,19)
    girls_age<- c(16,14,18,17,15)



    1. Then Append rbind() the two vectors to create data.frame such that I have two columns named group and age.


    2. The values from boys_age and girls_age should be in the column age. The group column should have the category values, boys/girls, to identify the source vector.











    share|improve this question



























      -1












      -1








      -1









      1. I am creating a vector having 5 age values and named it as boys_age. Likewise created a vector named girls_age.


      eg :



      boys_age <- c(18,15,16,17,19)
      girls_age<- c(16,14,18,17,15)



      1. Then Append rbind() the two vectors to create data.frame such that I have two columns named group and age.


      2. The values from boys_age and girls_age should be in the column age. The group column should have the category values, boys/girls, to identify the source vector.











      share|improve this question

















      1. I am creating a vector having 5 age values and named it as boys_age. Likewise created a vector named girls_age.


      eg :



      boys_age <- c(18,15,16,17,19)
      girls_age<- c(16,14,18,17,15)



      1. Then Append rbind() the two vectors to create data.frame such that I have two columns named group and age.


      2. The values from boys_age and girls_age should be in the column age. The group column should have the category values, boys/girls, to identify the source vector.








      r rbind






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 20 '18 at 9:28









      Sotos

      30k51640




      30k51640










      asked Nov 20 '18 at 9:26









      Himanshu GehlotHimanshu Gehlot

      13




      13
























          2 Answers
          2






          active

          oldest

          votes


















          2














          Its actually the most primal thing to do in R:



          data:



          df1 <- data.frame(boys_age = c(18,15,16,17,19), girls_age = c(16,14,18,17,15))


          code:



          library(data.table)
          melt(setDT(df1), variable.name = "group", value.name = "age", measure.vars = c("boys_age", "girls_age"))[,2:1][,group:=sub("_.*$","",group)]


          result:



          #    age group
          # 1: 18 boys
          # 2: 15 boys
          # 3: 16 boys
          # 4: 17 boys
          # 5: 19 boys
          # 6: 16 girls
          # 7: 14 girls
          # 8: 18 girls
          # 9: 17 girls
          #10: 15 girls




          You seem to be keen on using ?rbind: (not practical though)



          rbind(
          cbind.data.frame(age = df1$boys_age, group = "boys"),
          cbind.data.frame(age = df1$girls_age, group = "girls")
          )

          # age group
          #1 18 boys
          #2 15 boys
          #3 16 boys
          #4 17 boys
          #5 19 boys
          #6 16 girls
          #7 14 girls
          #8 18 girls
          #9 17 girls
          #10 15 girls



          • In the ?cbind section I'm making use of the recycling functionality R provides. Read about it.

          • Why am I using cbind.data.frame, otherwise cbind would create a matrix and therefore the age numerics would be converted to characters.






          share|improve this answer

































            3














            Here is one option using stack



            out <- stack(list(boys = boys_age, girls = girls_age))
            out
            # values ind
            #1 18 boys
            #2 15 boys
            #3 16 boys
            #4 17 boys
            #5 19 boys
            #6 16 girls
            #7 14 girls
            #8 18 girls
            #9 17 girls
            #10 15 girls


            Now change names



            names(out) <- c("age", "group")
            out
            # age group
            #1 18 boys
            #2 15 boys
            #3 16 boys
            #4 17 boys
            #5 19 boys
            #6 16 girls
            #7 14 girls
            #8 18 girls
            #9 17 girls
            #10 15 girls




            You could also do the same in one line, thanks to @Sotos



            setNames(stack(list(boys = boys_age, girls = girls_age)), c('age', 'group'))





            share|improve this answer





















            • 3





              Nice 1. You can also do all in 1 line, i.e. setNames(stack(list(boys = boys_age, girls = girls_age)), c('age', 'group'))

              – Sotos
              Nov 20 '18 at 9:37











            • Thank you, Markus, for your response. But the problem needs to be solved by using data.frame() and rbind() only. It's an exercise for data.frame and rbind function.

              – Himanshu Gehlot
              Nov 20 '18 at 9:39








            • 1





              That smells like homework...

              – Sotos
              Nov 20 '18 at 9:41











            • Not exactly, i am making some practice questions and got stuck with this.

              – Himanshu Gehlot
              Nov 20 '18 at 9:43






            • 2





              @HimanshuGehlot You can't do it directly with rbind() and data.frame() only. You could do something like setNames(stack(data.frame(t(rbind(boys_age, girls_age)))), c('age', 'group')) which includes them, but are not explicit

              – Sotos
              Nov 20 '18 at 10:00











            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%2f53389851%2ferror-in-match-namesclabs-namesxi-names-do-not-match-previous-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









            2














            Its actually the most primal thing to do in R:



            data:



            df1 <- data.frame(boys_age = c(18,15,16,17,19), girls_age = c(16,14,18,17,15))


            code:



            library(data.table)
            melt(setDT(df1), variable.name = "group", value.name = "age", measure.vars = c("boys_age", "girls_age"))[,2:1][,group:=sub("_.*$","",group)]


            result:



            #    age group
            # 1: 18 boys
            # 2: 15 boys
            # 3: 16 boys
            # 4: 17 boys
            # 5: 19 boys
            # 6: 16 girls
            # 7: 14 girls
            # 8: 18 girls
            # 9: 17 girls
            #10: 15 girls




            You seem to be keen on using ?rbind: (not practical though)



            rbind(
            cbind.data.frame(age = df1$boys_age, group = "boys"),
            cbind.data.frame(age = df1$girls_age, group = "girls")
            )

            # age group
            #1 18 boys
            #2 15 boys
            #3 16 boys
            #4 17 boys
            #5 19 boys
            #6 16 girls
            #7 14 girls
            #8 18 girls
            #9 17 girls
            #10 15 girls



            • In the ?cbind section I'm making use of the recycling functionality R provides. Read about it.

            • Why am I using cbind.data.frame, otherwise cbind would create a matrix and therefore the age numerics would be converted to characters.






            share|improve this answer






























              2














              Its actually the most primal thing to do in R:



              data:



              df1 <- data.frame(boys_age = c(18,15,16,17,19), girls_age = c(16,14,18,17,15))


              code:



              library(data.table)
              melt(setDT(df1), variable.name = "group", value.name = "age", measure.vars = c("boys_age", "girls_age"))[,2:1][,group:=sub("_.*$","",group)]


              result:



              #    age group
              # 1: 18 boys
              # 2: 15 boys
              # 3: 16 boys
              # 4: 17 boys
              # 5: 19 boys
              # 6: 16 girls
              # 7: 14 girls
              # 8: 18 girls
              # 9: 17 girls
              #10: 15 girls




              You seem to be keen on using ?rbind: (not practical though)



              rbind(
              cbind.data.frame(age = df1$boys_age, group = "boys"),
              cbind.data.frame(age = df1$girls_age, group = "girls")
              )

              # age group
              #1 18 boys
              #2 15 boys
              #3 16 boys
              #4 17 boys
              #5 19 boys
              #6 16 girls
              #7 14 girls
              #8 18 girls
              #9 17 girls
              #10 15 girls



              • In the ?cbind section I'm making use of the recycling functionality R provides. Read about it.

              • Why am I using cbind.data.frame, otherwise cbind would create a matrix and therefore the age numerics would be converted to characters.






              share|improve this answer




























                2












                2








                2







                Its actually the most primal thing to do in R:



                data:



                df1 <- data.frame(boys_age = c(18,15,16,17,19), girls_age = c(16,14,18,17,15))


                code:



                library(data.table)
                melt(setDT(df1), variable.name = "group", value.name = "age", measure.vars = c("boys_age", "girls_age"))[,2:1][,group:=sub("_.*$","",group)]


                result:



                #    age group
                # 1: 18 boys
                # 2: 15 boys
                # 3: 16 boys
                # 4: 17 boys
                # 5: 19 boys
                # 6: 16 girls
                # 7: 14 girls
                # 8: 18 girls
                # 9: 17 girls
                #10: 15 girls




                You seem to be keen on using ?rbind: (not practical though)



                rbind(
                cbind.data.frame(age = df1$boys_age, group = "boys"),
                cbind.data.frame(age = df1$girls_age, group = "girls")
                )

                # age group
                #1 18 boys
                #2 15 boys
                #3 16 boys
                #4 17 boys
                #5 19 boys
                #6 16 girls
                #7 14 girls
                #8 18 girls
                #9 17 girls
                #10 15 girls



                • In the ?cbind section I'm making use of the recycling functionality R provides. Read about it.

                • Why am I using cbind.data.frame, otherwise cbind would create a matrix and therefore the age numerics would be converted to characters.






                share|improve this answer















                Its actually the most primal thing to do in R:



                data:



                df1 <- data.frame(boys_age = c(18,15,16,17,19), girls_age = c(16,14,18,17,15))


                code:



                library(data.table)
                melt(setDT(df1), variable.name = "group", value.name = "age", measure.vars = c("boys_age", "girls_age"))[,2:1][,group:=sub("_.*$","",group)]


                result:



                #    age group
                # 1: 18 boys
                # 2: 15 boys
                # 3: 16 boys
                # 4: 17 boys
                # 5: 19 boys
                # 6: 16 girls
                # 7: 14 girls
                # 8: 18 girls
                # 9: 17 girls
                #10: 15 girls




                You seem to be keen on using ?rbind: (not practical though)



                rbind(
                cbind.data.frame(age = df1$boys_age, group = "boys"),
                cbind.data.frame(age = df1$girls_age, group = "girls")
                )

                # age group
                #1 18 boys
                #2 15 boys
                #3 16 boys
                #4 17 boys
                #5 19 boys
                #6 16 girls
                #7 14 girls
                #8 18 girls
                #9 17 girls
                #10 15 girls



                • In the ?cbind section I'm making use of the recycling functionality R provides. Read about it.

                • Why am I using cbind.data.frame, otherwise cbind would create a matrix and therefore the age numerics would be converted to characters.







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Nov 20 '18 at 11:29

























                answered Nov 20 '18 at 9:48









                Andre ElricoAndre Elrico

                5,72311229




                5,72311229

























                    3














                    Here is one option using stack



                    out <- stack(list(boys = boys_age, girls = girls_age))
                    out
                    # values ind
                    #1 18 boys
                    #2 15 boys
                    #3 16 boys
                    #4 17 boys
                    #5 19 boys
                    #6 16 girls
                    #7 14 girls
                    #8 18 girls
                    #9 17 girls
                    #10 15 girls


                    Now change names



                    names(out) <- c("age", "group")
                    out
                    # age group
                    #1 18 boys
                    #2 15 boys
                    #3 16 boys
                    #4 17 boys
                    #5 19 boys
                    #6 16 girls
                    #7 14 girls
                    #8 18 girls
                    #9 17 girls
                    #10 15 girls




                    You could also do the same in one line, thanks to @Sotos



                    setNames(stack(list(boys = boys_age, girls = girls_age)), c('age', 'group'))





                    share|improve this answer





















                    • 3





                      Nice 1. You can also do all in 1 line, i.e. setNames(stack(list(boys = boys_age, girls = girls_age)), c('age', 'group'))

                      – Sotos
                      Nov 20 '18 at 9:37











                    • Thank you, Markus, for your response. But the problem needs to be solved by using data.frame() and rbind() only. It's an exercise for data.frame and rbind function.

                      – Himanshu Gehlot
                      Nov 20 '18 at 9:39








                    • 1





                      That smells like homework...

                      – Sotos
                      Nov 20 '18 at 9:41











                    • Not exactly, i am making some practice questions and got stuck with this.

                      – Himanshu Gehlot
                      Nov 20 '18 at 9:43






                    • 2





                      @HimanshuGehlot You can't do it directly with rbind() and data.frame() only. You could do something like setNames(stack(data.frame(t(rbind(boys_age, girls_age)))), c('age', 'group')) which includes them, but are not explicit

                      – Sotos
                      Nov 20 '18 at 10:00
















                    3














                    Here is one option using stack



                    out <- stack(list(boys = boys_age, girls = girls_age))
                    out
                    # values ind
                    #1 18 boys
                    #2 15 boys
                    #3 16 boys
                    #4 17 boys
                    #5 19 boys
                    #6 16 girls
                    #7 14 girls
                    #8 18 girls
                    #9 17 girls
                    #10 15 girls


                    Now change names



                    names(out) <- c("age", "group")
                    out
                    # age group
                    #1 18 boys
                    #2 15 boys
                    #3 16 boys
                    #4 17 boys
                    #5 19 boys
                    #6 16 girls
                    #7 14 girls
                    #8 18 girls
                    #9 17 girls
                    #10 15 girls




                    You could also do the same in one line, thanks to @Sotos



                    setNames(stack(list(boys = boys_age, girls = girls_age)), c('age', 'group'))





                    share|improve this answer





















                    • 3





                      Nice 1. You can also do all in 1 line, i.e. setNames(stack(list(boys = boys_age, girls = girls_age)), c('age', 'group'))

                      – Sotos
                      Nov 20 '18 at 9:37











                    • Thank you, Markus, for your response. But the problem needs to be solved by using data.frame() and rbind() only. It's an exercise for data.frame and rbind function.

                      – Himanshu Gehlot
                      Nov 20 '18 at 9:39








                    • 1





                      That smells like homework...

                      – Sotos
                      Nov 20 '18 at 9:41











                    • Not exactly, i am making some practice questions and got stuck with this.

                      – Himanshu Gehlot
                      Nov 20 '18 at 9:43






                    • 2





                      @HimanshuGehlot You can't do it directly with rbind() and data.frame() only. You could do something like setNames(stack(data.frame(t(rbind(boys_age, girls_age)))), c('age', 'group')) which includes them, but are not explicit

                      – Sotos
                      Nov 20 '18 at 10:00














                    3












                    3








                    3







                    Here is one option using stack



                    out <- stack(list(boys = boys_age, girls = girls_age))
                    out
                    # values ind
                    #1 18 boys
                    #2 15 boys
                    #3 16 boys
                    #4 17 boys
                    #5 19 boys
                    #6 16 girls
                    #7 14 girls
                    #8 18 girls
                    #9 17 girls
                    #10 15 girls


                    Now change names



                    names(out) <- c("age", "group")
                    out
                    # age group
                    #1 18 boys
                    #2 15 boys
                    #3 16 boys
                    #4 17 boys
                    #5 19 boys
                    #6 16 girls
                    #7 14 girls
                    #8 18 girls
                    #9 17 girls
                    #10 15 girls




                    You could also do the same in one line, thanks to @Sotos



                    setNames(stack(list(boys = boys_age, girls = girls_age)), c('age', 'group'))





                    share|improve this answer















                    Here is one option using stack



                    out <- stack(list(boys = boys_age, girls = girls_age))
                    out
                    # values ind
                    #1 18 boys
                    #2 15 boys
                    #3 16 boys
                    #4 17 boys
                    #5 19 boys
                    #6 16 girls
                    #7 14 girls
                    #8 18 girls
                    #9 17 girls
                    #10 15 girls


                    Now change names



                    names(out) <- c("age", "group")
                    out
                    # age group
                    #1 18 boys
                    #2 15 boys
                    #3 16 boys
                    #4 17 boys
                    #5 19 boys
                    #6 16 girls
                    #7 14 girls
                    #8 18 girls
                    #9 17 girls
                    #10 15 girls




                    You could also do the same in one line, thanks to @Sotos



                    setNames(stack(list(boys = boys_age, girls = girls_age)), c('age', 'group'))






                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Nov 20 '18 at 9:39

























                    answered Nov 20 '18 at 9:33









                    markusmarkus

                    13.1k1234




                    13.1k1234








                    • 3





                      Nice 1. You can also do all in 1 line, i.e. setNames(stack(list(boys = boys_age, girls = girls_age)), c('age', 'group'))

                      – Sotos
                      Nov 20 '18 at 9:37











                    • Thank you, Markus, for your response. But the problem needs to be solved by using data.frame() and rbind() only. It's an exercise for data.frame and rbind function.

                      – Himanshu Gehlot
                      Nov 20 '18 at 9:39








                    • 1





                      That smells like homework...

                      – Sotos
                      Nov 20 '18 at 9:41











                    • Not exactly, i am making some practice questions and got stuck with this.

                      – Himanshu Gehlot
                      Nov 20 '18 at 9:43






                    • 2





                      @HimanshuGehlot You can't do it directly with rbind() and data.frame() only. You could do something like setNames(stack(data.frame(t(rbind(boys_age, girls_age)))), c('age', 'group')) which includes them, but are not explicit

                      – Sotos
                      Nov 20 '18 at 10:00














                    • 3





                      Nice 1. You can also do all in 1 line, i.e. setNames(stack(list(boys = boys_age, girls = girls_age)), c('age', 'group'))

                      – Sotos
                      Nov 20 '18 at 9:37











                    • Thank you, Markus, for your response. But the problem needs to be solved by using data.frame() and rbind() only. It's an exercise for data.frame and rbind function.

                      – Himanshu Gehlot
                      Nov 20 '18 at 9:39








                    • 1





                      That smells like homework...

                      – Sotos
                      Nov 20 '18 at 9:41











                    • Not exactly, i am making some practice questions and got stuck with this.

                      – Himanshu Gehlot
                      Nov 20 '18 at 9:43






                    • 2





                      @HimanshuGehlot You can't do it directly with rbind() and data.frame() only. You could do something like setNames(stack(data.frame(t(rbind(boys_age, girls_age)))), c('age', 'group')) which includes them, but are not explicit

                      – Sotos
                      Nov 20 '18 at 10:00








                    3




                    3





                    Nice 1. You can also do all in 1 line, i.e. setNames(stack(list(boys = boys_age, girls = girls_age)), c('age', 'group'))

                    – Sotos
                    Nov 20 '18 at 9:37





                    Nice 1. You can also do all in 1 line, i.e. setNames(stack(list(boys = boys_age, girls = girls_age)), c('age', 'group'))

                    – Sotos
                    Nov 20 '18 at 9:37













                    Thank you, Markus, for your response. But the problem needs to be solved by using data.frame() and rbind() only. It's an exercise for data.frame and rbind function.

                    – Himanshu Gehlot
                    Nov 20 '18 at 9:39







                    Thank you, Markus, for your response. But the problem needs to be solved by using data.frame() and rbind() only. It's an exercise for data.frame and rbind function.

                    – Himanshu Gehlot
                    Nov 20 '18 at 9:39






                    1




                    1





                    That smells like homework...

                    – Sotos
                    Nov 20 '18 at 9:41





                    That smells like homework...

                    – Sotos
                    Nov 20 '18 at 9:41













                    Not exactly, i am making some practice questions and got stuck with this.

                    – Himanshu Gehlot
                    Nov 20 '18 at 9:43





                    Not exactly, i am making some practice questions and got stuck with this.

                    – Himanshu Gehlot
                    Nov 20 '18 at 9:43




                    2




                    2





                    @HimanshuGehlot You can't do it directly with rbind() and data.frame() only. You could do something like setNames(stack(data.frame(t(rbind(boys_age, girls_age)))), c('age', 'group')) which includes them, but are not explicit

                    – Sotos
                    Nov 20 '18 at 10:00





                    @HimanshuGehlot You can't do it directly with rbind() and data.frame() only. You could do something like setNames(stack(data.frame(t(rbind(boys_age, girls_age)))), c('age', 'group')) which includes them, but are not explicit

                    – Sotos
                    Nov 20 '18 at 10:00


















                    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%2f53389851%2ferror-in-match-namesclabs-namesxi-names-do-not-match-previous-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

                    Guess what letter conforming each word

                    Port of Spain

                    Run scheduled task as local user group (not BUILTIN)