Create an new column(s) after melting a DT according to a ref. DT
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
add a comment |
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
add a comment |
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
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
r data.table
asked Nov 13 '18 at 21:21
akh22akh22
573
573
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
Something like below?
setDT(df_reference)[setDT(df_melted), on = 'SampleID']
Or just df_reference[df_melted, on = 'SampleID']
if they're already data.table
s.
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
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Something like below?
setDT(df_reference)[setDT(df_melted), on = 'SampleID']
Or just df_reference[df_melted, on = 'SampleID']
if they're already data.table
s.
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
add a comment |
Something like below?
setDT(df_reference)[setDT(df_melted), on = 'SampleID']
Or just df_reference[df_melted, on = 'SampleID']
if they're already data.table
s.
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
add a comment |
Something like below?
setDT(df_reference)[setDT(df_melted), on = 'SampleID']
Or just df_reference[df_melted, on = 'SampleID']
if they're already data.table
s.
Something like below?
setDT(df_reference)[setDT(df_melted), on = 'SampleID']
Or just df_reference[df_melted, on = 'SampleID']
if they're already data.table
s.
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
add a comment |
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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