Rearranging data frame from column names to key value pairs Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm 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

Why are two-digit numbers in Jonathan Swift's "Gulliver's Travels" (1726) written in "German style"?

Why did Bronn offer to be Tyrion Lannister's champion in trial by combat?

The Nth Gryphon Number

Vertical ranges of Column Plots in 12

How does the body cool itself in a stillsuit?

Russian equivalents of おしゃれは足元から (Every good outfit starts with the shoes)

Diophantine equation 3^a+1=3^b+5^c

Why is there so little support for joining EFTA in the British parliament?

Flight departed from the gate 5 min before scheduled departure time. Refund options

What are some likely causes to domain member PC losing contact to domain controller?

Does the main washing effect of soap come from foam?

How do I say "this must not happen"?

Twin's vs. Twins'

How could a hydrazine and N2O4 cloud (or it's reactants) show up in weather radar?

What is "Lambda" in Heston's original paper on stochastic volatility models?

Can two people see the same photon?

Is there a spell that can create a permanent fire?

Does the universe have a fixed centre of mass?

Why are current probes so expensive?

Centre cell vertically in tabularx

malloc in main() or malloc in another function: allocating memory for a struct and its members

What did Turing mean when saying that "machines cannot give rise to surprises" is due to a fallacy?

What is a more techy Technical Writer job title that isn't cutesy or confusing?

Adapting the Chinese Remainder Theorem (CRT) for integers to polynomials



Rearranging data frame from column names to key value pairs



Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm 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