Create an new column(s) after melting a DT according to a ref. DT










0















I have this melted DT ;



 Gene SampleID value
1 Gene1 T26 0.06698887
2 Gene2 T26 1.09944463
3 Gene3 T26 3.63930176
4 Gene1 T27 0.84548702
5 Gene2 T27 -1.49075354
6 Gene3 T27 4.61516518


and a sample reference DT;



 SampleID Batch Disease Infection
1: T26 1 Control No
2: T27 2 Disease Yes


DT I'd like to add columns from the ref. DT to the melted DTs 'on="SampleID"' is following;



 Gene SampleID value Batch Disease Infection
1 Gene1 T26 0.06698887 1 Control No
2 Gene2 T26 1.09944463 1 Control No
3 Gene3 T26 3.63930176 1 Control No
4 Gene1 T27 0.84548702 2 Disease Yes


and so on. I tried this with := but DT complained that nrow between two DT are not the same. I've using a 'setcolumn' trick that I learned from the previous question but it is no desirable. I am looking for a DT one liner solution and any help/pointer will be appreciated.










share|improve this question


























    0















    I have this melted DT ;



     Gene SampleID value
    1 Gene1 T26 0.06698887
    2 Gene2 T26 1.09944463
    3 Gene3 T26 3.63930176
    4 Gene1 T27 0.84548702
    5 Gene2 T27 -1.49075354
    6 Gene3 T27 4.61516518


    and a sample reference DT;



     SampleID Batch Disease Infection
    1: T26 1 Control No
    2: T27 2 Disease Yes


    DT I'd like to add columns from the ref. DT to the melted DTs 'on="SampleID"' is following;



     Gene SampleID value Batch Disease Infection
    1 Gene1 T26 0.06698887 1 Control No
    2 Gene2 T26 1.09944463 1 Control No
    3 Gene3 T26 3.63930176 1 Control No
    4 Gene1 T27 0.84548702 2 Disease Yes


    and so on. I tried this with := but DT complained that nrow between two DT are not the same. I've using a 'setcolumn' trick that I learned from the previous question but it is no desirable. I am looking for a DT one liner solution and any help/pointer will be appreciated.










    share|improve this question
























      0












      0








      0


      1






      I have this melted DT ;



       Gene SampleID value
      1 Gene1 T26 0.06698887
      2 Gene2 T26 1.09944463
      3 Gene3 T26 3.63930176
      4 Gene1 T27 0.84548702
      5 Gene2 T27 -1.49075354
      6 Gene3 T27 4.61516518


      and a sample reference DT;



       SampleID Batch Disease Infection
      1: T26 1 Control No
      2: T27 2 Disease Yes


      DT I'd like to add columns from the ref. DT to the melted DTs 'on="SampleID"' is following;



       Gene SampleID value Batch Disease Infection
      1 Gene1 T26 0.06698887 1 Control No
      2 Gene2 T26 1.09944463 1 Control No
      3 Gene3 T26 3.63930176 1 Control No
      4 Gene1 T27 0.84548702 2 Disease Yes


      and so on. I tried this with := but DT complained that nrow between two DT are not the same. I've using a 'setcolumn' trick that I learned from the previous question but it is no desirable. I am looking for a DT one liner solution and any help/pointer will be appreciated.










      share|improve this question














      I have this melted DT ;



       Gene SampleID value
      1 Gene1 T26 0.06698887
      2 Gene2 T26 1.09944463
      3 Gene3 T26 3.63930176
      4 Gene1 T27 0.84548702
      5 Gene2 T27 -1.49075354
      6 Gene3 T27 4.61516518


      and a sample reference DT;



       SampleID Batch Disease Infection
      1: T26 1 Control No
      2: T27 2 Disease Yes


      DT I'd like to add columns from the ref. DT to the melted DTs 'on="SampleID"' is following;



       Gene SampleID value Batch Disease Infection
      1 Gene1 T26 0.06698887 1 Control No
      2 Gene2 T26 1.09944463 1 Control No
      3 Gene3 T26 3.63930176 1 Control No
      4 Gene1 T27 0.84548702 2 Disease Yes


      and so on. I tried this with := but DT complained that nrow between two DT are not the same. I've using a 'setcolumn' trick that I learned from the previous question but it is no desirable. I am looking for a DT one liner solution and any help/pointer will be appreciated.







      r data.table






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 13 '18 at 21:21









      akh22akh22

      573




      573






















          1 Answer
          1






          active

          oldest

          votes


















          0














          Something like below?



          setDT(df_reference)[setDT(df_melted), on = 'SampleID']


          Or just df_reference[df_melted, on = 'SampleID'] if they're already data.tables.






          share|improve this answer























          • I should've known that the inner-join would work here !!

            – akh22
            Nov 15 '18 at 15:46











          • Glad it works! Consider accepting the answer if you found it helpful.

            – arg0naut
            Nov 15 '18 at 15:50










          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%2f53289665%2fcreate-an-new-columns-after-melting-a-dt-according-to-a-ref-dt%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          Something like below?



          setDT(df_reference)[setDT(df_melted), on = 'SampleID']


          Or just df_reference[df_melted, on = 'SampleID'] if they're already data.tables.






          share|improve this answer























          • I should've known that the inner-join would work here !!

            – akh22
            Nov 15 '18 at 15:46











          • Glad it works! Consider accepting the answer if you found it helpful.

            – arg0naut
            Nov 15 '18 at 15:50















          0














          Something like below?



          setDT(df_reference)[setDT(df_melted), on = 'SampleID']


          Or just df_reference[df_melted, on = 'SampleID'] if they're already data.tables.






          share|improve this answer























          • I should've known that the inner-join would work here !!

            – akh22
            Nov 15 '18 at 15:46











          • Glad it works! Consider accepting the answer if you found it helpful.

            – arg0naut
            Nov 15 '18 at 15:50













          0












          0








          0







          Something like below?



          setDT(df_reference)[setDT(df_melted), on = 'SampleID']


          Or just df_reference[df_melted, on = 'SampleID'] if they're already data.tables.






          share|improve this answer













          Something like below?



          setDT(df_reference)[setDT(df_melted), on = 'SampleID']


          Or just df_reference[df_melted, on = 'SampleID'] if they're already data.tables.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 13 '18 at 21:32









          arg0nautarg0naut

          4,2191316




          4,2191316












          • I should've known that the inner-join would work here !!

            – akh22
            Nov 15 '18 at 15:46











          • Glad it works! Consider accepting the answer if you found it helpful.

            – arg0naut
            Nov 15 '18 at 15:50

















          • I should've known that the inner-join would work here !!

            – akh22
            Nov 15 '18 at 15:46











          • Glad it works! Consider accepting the answer if you found it helpful.

            – arg0naut
            Nov 15 '18 at 15:50
















          I should've known that the inner-join would work here !!

          – akh22
          Nov 15 '18 at 15:46





          I should've known that the inner-join would work here !!

          – akh22
          Nov 15 '18 at 15:46













          Glad it works! Consider accepting the answer if you found it helpful.

          – arg0naut
          Nov 15 '18 at 15:50





          Glad it works! Consider accepting the answer if you found it helpful.

          – arg0naut
          Nov 15 '18 at 15:50



















          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%2f53289665%2fcreate-an-new-columns-after-melting-a-dt-according-to-a-ref-dt%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

          Darth Vader #20

          How to how show current date and time by default on contact form 7 in WordPress without taking input from user in datetimepicker

          Ondo