Rearranging data frame from column names to key value pairs Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern) 2019 Moderator Election Q&A - Questionnaire 2019 Community Moderator Election Resultssubsetting R data frameData frame mutation in RHow to cluster multiple time-series from one data frameHow to plot two distribution curves in a faceted way in R/ggplot2?Retrieving column names in RSlice rows in R based on column valueHow do i convert a Named num [1:4] to a data frame in R?How can I sort a data frame by groups?R - How do I remove a varying number of digits from a date-vectorExtract or subset hundreds of columns from a data frame

Project Euler #1 in C++

Why wasn't DOSKEY integrated with COMMAND.COM?

What is the meaning of the simile “quick as silk”?

How fail-safe is nr as stop bytes?

How to convince students of the implication truth values?

Can family of EU Blue Card holder travel freely in the Schengen Area with a German Aufenthaltstitel?

How to react to hostile behavior from a senior developer?

Is there any word for a place full of confusion?

Would "destroying" Wurmcoil Engine prevent its tokens from being created?

What do you call the main part of a joke?

AppleTVs create a chatty alternate WiFi network

Dating a Former Employee

Century handling in Pandas

Is the Standard Deduction better than Itemized when both are the same amount?

Does the Weapon Master feat grant you a fighting style?

What causes the direction of lightning flashes?

Can a new player join a group only when a new campaign starts?

Denied boarding although I have proper visa and documentation. To whom should I make a complaint?

Is this homebrew Lady of Pain warlock patron balanced?

Selecting user stories during sprint planning

What font is "z" in "z-score"?

Is it a good idea to use CNN to classify 1D signal?

Amount of permutations on an NxNxN Rubik's Cube

How to tell that you are a giant?



Rearranging data frame from column names to key value pairs



Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern)
2019 Moderator Election Q&A - Questionnaire
2019 Community Moderator Election Resultssubsetting R data frameData frame mutation in RHow to cluster multiple time-series from one data frameHow to plot two distribution curves in a faceted way in R/ggplot2?Retrieving column names in RSlice rows in R based on column valueHow do i convert a Named num [1:4] to a data frame in R?How can I sort a data frame by groups?R - How do I remove a varying number of digits from a date-vectorExtract or subset hundreds of columns from a data frame










3












$begingroup$


If I have data like this in R:



> a = data.frame(a = c(1, 2),
b = c(3, 4))
> a
a b
1 1 3
2 2 4


but I would like to have it like this:



> b = data.frame(k = c("a", "a", "b", "b"),
+ v = c(1, 2, 3, 4))
> b
k v
1 a 1
2 a 2
3 b 3
4 b 4



How can I convert the data this way?










share|improve this question











$endgroup$
















    3












    $begingroup$


    If I have data like this in R:



    > a = data.frame(a = c(1, 2),
    b = c(3, 4))
    > a
    a b
    1 1 3
    2 2 4


    but I would like to have it like this:



    > b = data.frame(k = c("a", "a", "b", "b"),
    + v = c(1, 2, 3, 4))
    > b
    k v
    1 a 1
    2 a 2
    3 b 3
    4 b 4



    How can I convert the data this way?










    share|improve this question











    $endgroup$














      3












      3








      3





      $begingroup$


      If I have data like this in R:



      > a = data.frame(a = c(1, 2),
      b = c(3, 4))
      > a
      a b
      1 1 3
      2 2 4


      but I would like to have it like this:



      > b = data.frame(k = c("a", "a", "b", "b"),
      + v = c(1, 2, 3, 4))
      > b
      k v
      1 a 1
      2 a 2
      3 b 3
      4 b 4



      How can I convert the data this way?










      share|improve this question











      $endgroup$




      If I have data like this in R:



      > a = data.frame(a = c(1, 2),
      b = c(3, 4))
      > a
      a b
      1 1 3
      2 2 4


      but I would like to have it like this:



      > b = data.frame(k = c("a", "a", "b", "b"),
      + v = c(1, 2, 3, 4))
      > b
      k v
      1 a 1
      2 a 2
      3 b 3
      4 b 4



      How can I convert the data this way?







      r data






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Apr 4 at 14:06









      Stephen Rauch

      1,52551330




      1,52551330










      asked Apr 4 at 13:14









      MartinMartin

      183




      183




















          2 Answers
          2






          active

          oldest

          votes


















          2












          $begingroup$

          This is a simple gather() using package tidyr:



          > library(tidyr)
          >
          > gather(a, key = "k", value = "v")
          k v
          1 a 1
          2 a 2
          3 b 3
          4 b 4





          share|improve this answer









          $endgroup$




















            2












            $begingroup$

            You can alternatively use the melt() function from the reshape2 package



            > library(reshape2)
            >
            > melt(a, variable.name="k", value.name="v")
            No id variables; using all as measure variables
            k v
            1 a 1
            2 a 2
            3 b 3
            4 b 4


            Hope this is insightful!






            share|improve this answer









            $endgroup$













              Your Answer








              StackExchange.ready(function()
              var channelOptions =
              tags: "".split(" "),
              id: "557"
              ;
              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: false,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: null,
              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%2fdatascience.stackexchange.com%2fquestions%2f48609%2frearranging-data-frame-from-column-names-to-key-value-pairs%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












              $begingroup$

              This is a simple gather() using package tidyr:



              > library(tidyr)
              >
              > gather(a, key = "k", value = "v")
              k v
              1 a 1
              2 a 2
              3 b 3
              4 b 4





              share|improve this answer









              $endgroup$

















                2












                $begingroup$

                This is a simple gather() using package tidyr:



                > library(tidyr)
                >
                > gather(a, key = "k", value = "v")
                k v
                1 a 1
                2 a 2
                3 b 3
                4 b 4





                share|improve this answer









                $endgroup$















                  2












                  2








                  2





                  $begingroup$

                  This is a simple gather() using package tidyr:



                  > library(tidyr)
                  >
                  > gather(a, key = "k", value = "v")
                  k v
                  1 a 1
                  2 a 2
                  3 b 3
                  4 b 4





                  share|improve this answer









                  $endgroup$



                  This is a simple gather() using package tidyr:



                  > library(tidyr)
                  >
                  > gather(a, key = "k", value = "v")
                  k v
                  1 a 1
                  2 a 2
                  3 b 3
                  4 b 4






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Apr 4 at 15:09









                  AdamAdam

                  1363




                  1363





















                      2












                      $begingroup$

                      You can alternatively use the melt() function from the reshape2 package



                      > library(reshape2)
                      >
                      > melt(a, variable.name="k", value.name="v")
                      No id variables; using all as measure variables
                      k v
                      1 a 1
                      2 a 2
                      3 b 3
                      4 b 4


                      Hope this is insightful!






                      share|improve this answer









                      $endgroup$

















                        2












                        $begingroup$

                        You can alternatively use the melt() function from the reshape2 package



                        > library(reshape2)
                        >
                        > melt(a, variable.name="k", value.name="v")
                        No id variables; using all as measure variables
                        k v
                        1 a 1
                        2 a 2
                        3 b 3
                        4 b 4


                        Hope this is insightful!






                        share|improve this answer









                        $endgroup$















                          2












                          2








                          2





                          $begingroup$

                          You can alternatively use the melt() function from the reshape2 package



                          > library(reshape2)
                          >
                          > melt(a, variable.name="k", value.name="v")
                          No id variables; using all as measure variables
                          k v
                          1 a 1
                          2 a 2
                          3 b 3
                          4 b 4


                          Hope this is insightful!






                          share|improve this answer









                          $endgroup$



                          You can alternatively use the melt() function from the reshape2 package



                          > library(reshape2)
                          >
                          > melt(a, variable.name="k", value.name="v")
                          No id variables; using all as measure variables
                          k v
                          1 a 1
                          2 a 2
                          3 b 3
                          4 b 4


                          Hope this is insightful!







                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Apr 4 at 15:40









                          I'mAnAccountantIKnowAlotOfMathI'mAnAccountantIKnowAlotOfMath

                          1213




                          1213



























                              draft saved

                              draft discarded
















































                              Thanks for contributing an answer to Data Science Stack Exchange!


                              • 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.

                              Use MathJax to format equations. MathJax reference.


                              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%2fdatascience.stackexchange.com%2fquestions%2f48609%2frearranging-data-frame-from-column-names-to-key-value-pairs%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

                              Adding axes to figuresAdding axes labels to LaTeX figuresLaTeX equivalent of ConTeXt buffersRotate a node but not its content: the case of the ellipse decorationHow to define the default vertical distance between nodes?TikZ scaling graphic and adjust node position and keep font sizeNumerical conditional within tikz keys?adding axes to shapesAlign axes across subfiguresAdding figures with a certain orderLine up nested tikz enviroments or how to get rid of themAdding axes labels to LaTeX figures

                              Luettelo Yhdysvaltain laivaston lentotukialuksista Lähteet | Navigointivalikko

                              Gary (muusikko) Sisällysluettelo Historia | Rockin' High | Lähteet | Aiheesta muualla | NavigointivalikkoInfobox OKTuomas "Gary" Keskinen Ancaran kitaristiksiProjekti Rockin' High