Join datatables using more than one column name stored in one variable2019 Community Moderator ElectionJoin datatables using column names stored in variablesDrop data frame columns by nameHow can one work fully generically in data.table in R with column names in variablesSubsetting data in datatable in R using i with paste commandAdd columns to a data.table with joins efficientlyr data.table conditional sum referencing / performing a lookup from another separate data.tableJoin single variable to multiple variables in r data.tablerbindlist data.tables different dimensionsObvious `merge` leads to NA's. How to debug differences between data.tables?Combine two data tables in R by a condition referring to two columnsdata.table merge on partial match of different columns in R
Why would a jet engine that runs at temps excess of 2000°C burn when it crashes?
infinitive telling the purpose
A three room house but a three headED dog
Why would one plane in this picture not have gear down yet?
Why does the negative sign arise in this thermodynamic relation?
Subset counting for even numbers
Do items de-spawn in Diablo?
How could our ancestors have domesticated a solitary predator?
Are the terms "stab" and "staccato" synonyms?
Do I really need to have a scientific explanation for my premise?
The bar has been raised
Does splitting a potentially monolithic application into several smaller ones help prevent bugs?
Should I take out a loan for a friend to invest on my behalf?
Why the color red for the Republican Party
Can someone explain what is being said here in color publishing in the American Mathematical Monthly?
"One can do his homework in the library"
Time travel short story where dinosaur doesn't taste like chicken
Placing subfig vertically
Is having access to past exams cheating and, if yes, could it be proven just by a good grade?
Space in array system equations
Is Gradient Descent central to every optimizer?
Is there an equal sign with wider gap?
Rejected in 4th interview round citing insufficient years of experience
Are babies of evil humanoid species inherently evil?
Join datatables using more than one column name stored in one variable
2019 Community Moderator ElectionJoin datatables using column names stored in variablesDrop data frame columns by nameHow can one work fully generically in data.table in R with column names in variablesSubsetting data in datatable in R using i with paste commandAdd columns to a data.table with joins efficientlyr data.table conditional sum referencing / performing a lookup from another separate data.tableJoin single variable to multiple variables in r data.tablerbindlist data.tables different dimensionsObvious `merge` leads to NA's. How to debug differences between data.tables?Combine two data tables in R by a condition referring to two columnsdata.table merge on partial match of different columns in R
I am trying to perform an update join of two data tables with the fields (more than one) I need to use to join stored in a variable. Below is an example:
library(data.table)
DT1 <- data.table(col1 = 1:5, col2 = 5:1, lett = letters[1:5])
DT2 <- data.table(col1 = c(1:3, 2:5, 1), col2 = c(5:3, 4:1, 5))
joinFields <- c('col1', 'col2')
I tried doing it this way:
DT1[DT2,
on=c(paste0(joinFields, '=', joinFields)),
nomatch=0L]
This way is based on a solution suggested in Join datatables using column names stored in variables.
dt1[dt2_temp,
on=c(paste0(varName, ">valueMin"), paste0(varName, "<=valueMax")),
nomatch=0L]
It does not work. Obviously, my case is a bit different, because in the example I used, there are 2 pastes. Is there a solution that continues to allow me using on = c()
?
Edit: I am aware I can do it with merge
()`
r data.table
add a comment |
I am trying to perform an update join of two data tables with the fields (more than one) I need to use to join stored in a variable. Below is an example:
library(data.table)
DT1 <- data.table(col1 = 1:5, col2 = 5:1, lett = letters[1:5])
DT2 <- data.table(col1 = c(1:3, 2:5, 1), col2 = c(5:3, 4:1, 5))
joinFields <- c('col1', 'col2')
I tried doing it this way:
DT1[DT2,
on=c(paste0(joinFields, '=', joinFields)),
nomatch=0L]
This way is based on a solution suggested in Join datatables using column names stored in variables.
dt1[dt2_temp,
on=c(paste0(varName, ">valueMin"), paste0(varName, "<=valueMax")),
nomatch=0L]
It does not work. Obviously, my case is a bit different, because in the example I used, there are 2 pastes. Is there a solution that continues to allow me using on = c()
?
Edit: I am aware I can do it with merge
()`
r data.table
2
You mention "update join" but don't use:=
. Could you show and explain the desired output? Btw, you might need"=="
not"="
there, I guess, egDT1[DT2, on=sprintf("%s==%s", joinFields, joinFields)]
– Frank
Mar 6 at 16:40
2
the keys used to join appears to be the same in both tables so why notDT1[DT2, on=joinFields, nomatch=0L]
?
– chinsoon12
Mar 7 at 0:47
1
@Frank, thanks for your suggestion. It is indeed not an update join. I was a bit lazy and copied from the other thread. I should have addedlet := i.lett
– koteletje
Mar 7 at 14:01
@chinsoon12, thanks that is a very short solution
– koteletje
Mar 7 at 14:01
add a comment |
I am trying to perform an update join of two data tables with the fields (more than one) I need to use to join stored in a variable. Below is an example:
library(data.table)
DT1 <- data.table(col1 = 1:5, col2 = 5:1, lett = letters[1:5])
DT2 <- data.table(col1 = c(1:3, 2:5, 1), col2 = c(5:3, 4:1, 5))
joinFields <- c('col1', 'col2')
I tried doing it this way:
DT1[DT2,
on=c(paste0(joinFields, '=', joinFields)),
nomatch=0L]
This way is based on a solution suggested in Join datatables using column names stored in variables.
dt1[dt2_temp,
on=c(paste0(varName, ">valueMin"), paste0(varName, "<=valueMax")),
nomatch=0L]
It does not work. Obviously, my case is a bit different, because in the example I used, there are 2 pastes. Is there a solution that continues to allow me using on = c()
?
Edit: I am aware I can do it with merge
()`
r data.table
I am trying to perform an update join of two data tables with the fields (more than one) I need to use to join stored in a variable. Below is an example:
library(data.table)
DT1 <- data.table(col1 = 1:5, col2 = 5:1, lett = letters[1:5])
DT2 <- data.table(col1 = c(1:3, 2:5, 1), col2 = c(5:3, 4:1, 5))
joinFields <- c('col1', 'col2')
I tried doing it this way:
DT1[DT2,
on=c(paste0(joinFields, '=', joinFields)),
nomatch=0L]
This way is based on a solution suggested in Join datatables using column names stored in variables.
dt1[dt2_temp,
on=c(paste0(varName, ">valueMin"), paste0(varName, "<=valueMax")),
nomatch=0L]
It does not work. Obviously, my case is a bit different, because in the example I used, there are 2 pastes. Is there a solution that continues to allow me using on = c()
?
Edit: I am aware I can do it with merge
()`
r data.table
r data.table
edited Mar 6 at 16:39
Frank
55.4k659134
55.4k659134
asked Mar 6 at 16:27
koteletjekoteletje
1688
1688
2
You mention "update join" but don't use:=
. Could you show and explain the desired output? Btw, you might need"=="
not"="
there, I guess, egDT1[DT2, on=sprintf("%s==%s", joinFields, joinFields)]
– Frank
Mar 6 at 16:40
2
the keys used to join appears to be the same in both tables so why notDT1[DT2, on=joinFields, nomatch=0L]
?
– chinsoon12
Mar 7 at 0:47
1
@Frank, thanks for your suggestion. It is indeed not an update join. I was a bit lazy and copied from the other thread. I should have addedlet := i.lett
– koteletje
Mar 7 at 14:01
@chinsoon12, thanks that is a very short solution
– koteletje
Mar 7 at 14:01
add a comment |
2
You mention "update join" but don't use:=
. Could you show and explain the desired output? Btw, you might need"=="
not"="
there, I guess, egDT1[DT2, on=sprintf("%s==%s", joinFields, joinFields)]
– Frank
Mar 6 at 16:40
2
the keys used to join appears to be the same in both tables so why notDT1[DT2, on=joinFields, nomatch=0L]
?
– chinsoon12
Mar 7 at 0:47
1
@Frank, thanks for your suggestion. It is indeed not an update join. I was a bit lazy and copied from the other thread. I should have addedlet := i.lett
– koteletje
Mar 7 at 14:01
@chinsoon12, thanks that is a very short solution
– koteletje
Mar 7 at 14:01
2
2
You mention "update join" but don't use
:=
. Could you show and explain the desired output? Btw, you might need "=="
not "="
there, I guess, eg DT1[DT2, on=sprintf("%s==%s", joinFields, joinFields)]
– Frank
Mar 6 at 16:40
You mention "update join" but don't use
:=
. Could you show and explain the desired output? Btw, you might need "=="
not "="
there, I guess, eg DT1[DT2, on=sprintf("%s==%s", joinFields, joinFields)]
– Frank
Mar 6 at 16:40
2
2
the keys used to join appears to be the same in both tables so why not
DT1[DT2, on=joinFields, nomatch=0L]
?– chinsoon12
Mar 7 at 0:47
the keys used to join appears to be the same in both tables so why not
DT1[DT2, on=joinFields, nomatch=0L]
?– chinsoon12
Mar 7 at 0:47
1
1
@Frank, thanks for your suggestion. It is indeed not an update join. I was a bit lazy and copied from the other thread. I should have added
let := i.lett
– koteletje
Mar 7 at 14:01
@Frank, thanks for your suggestion. It is indeed not an update join. I was a bit lazy and copied from the other thread. I should have added
let := i.lett
– koteletje
Mar 7 at 14:01
@chinsoon12, thanks that is a very short solution
– koteletje
Mar 7 at 14:01
@chinsoon12, thanks that is a very short solution
– koteletje
Mar 7 at 14:01
add a comment |
1 Answer
1
active
oldest
votes
I think you just need to put two ==, as following:
DT1[DT2,
on=c(paste0(joinFields, '==', joinFields)),
nomatch=0L]
# col1 col2 lett
# 1: 1 5 a
# 2: 2 4 b
# 3: 3 3 c
# 4: 2 4 b
# 5: 3 3 c
# 6: 4 2 d
# 7: 5 1 e
# 8: 1 5 a
Even you do not need to use c()
:
DT1[DT2,
on=paste0(joinFields, '==', joinFields),
nomatch=0L]
# col1 col2 lett
# 1: 1 5 a
# 2: 2 4 b
# 3: 3 3 c
# 4: 2 4 b
# 5: 3 3 c
# 6: 4 2 d
# 7: 5 1 e
# 8: 1 5 a
Thank you very much @Carles Sans Fuentes. I must say I have some things to look into, because I don't understand why it works with the==
operator. I generally join like thisDT1[DT2, on = .(col1 = col1, col2 = col2), nomatch=0L]
– koteletje
Mar 7 at 6:52
2
@koteletje It's just the documented behavior with strings inon=
(from?data.table
)
– Frank
Mar 7 at 15:16
Happy to help @koteletje. We all need to be checking staff. Regarding your question of why it is two equals: besides being stated like that on thedata.table
documentation, the standard way to check if an object/something is exactly equal than another object/something is using logical operators, and it is stablished as two equals . Check this link for further understanding of it: statmethods.net/management/operators.html
– Carles Sans Fuentes
Mar 7 at 16:32
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%2f55027851%2fjoin-datatables-using-more-than-one-column-name-stored-in-one-variable%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
I think you just need to put two ==, as following:
DT1[DT2,
on=c(paste0(joinFields, '==', joinFields)),
nomatch=0L]
# col1 col2 lett
# 1: 1 5 a
# 2: 2 4 b
# 3: 3 3 c
# 4: 2 4 b
# 5: 3 3 c
# 6: 4 2 d
# 7: 5 1 e
# 8: 1 5 a
Even you do not need to use c()
:
DT1[DT2,
on=paste0(joinFields, '==', joinFields),
nomatch=0L]
# col1 col2 lett
# 1: 1 5 a
# 2: 2 4 b
# 3: 3 3 c
# 4: 2 4 b
# 5: 3 3 c
# 6: 4 2 d
# 7: 5 1 e
# 8: 1 5 a
Thank you very much @Carles Sans Fuentes. I must say I have some things to look into, because I don't understand why it works with the==
operator. I generally join like thisDT1[DT2, on = .(col1 = col1, col2 = col2), nomatch=0L]
– koteletje
Mar 7 at 6:52
2
@koteletje It's just the documented behavior with strings inon=
(from?data.table
)
– Frank
Mar 7 at 15:16
Happy to help @koteletje. We all need to be checking staff. Regarding your question of why it is two equals: besides being stated like that on thedata.table
documentation, the standard way to check if an object/something is exactly equal than another object/something is using logical operators, and it is stablished as two equals . Check this link for further understanding of it: statmethods.net/management/operators.html
– Carles Sans Fuentes
Mar 7 at 16:32
add a comment |
I think you just need to put two ==, as following:
DT1[DT2,
on=c(paste0(joinFields, '==', joinFields)),
nomatch=0L]
# col1 col2 lett
# 1: 1 5 a
# 2: 2 4 b
# 3: 3 3 c
# 4: 2 4 b
# 5: 3 3 c
# 6: 4 2 d
# 7: 5 1 e
# 8: 1 5 a
Even you do not need to use c()
:
DT1[DT2,
on=paste0(joinFields, '==', joinFields),
nomatch=0L]
# col1 col2 lett
# 1: 1 5 a
# 2: 2 4 b
# 3: 3 3 c
# 4: 2 4 b
# 5: 3 3 c
# 6: 4 2 d
# 7: 5 1 e
# 8: 1 5 a
Thank you very much @Carles Sans Fuentes. I must say I have some things to look into, because I don't understand why it works with the==
operator. I generally join like thisDT1[DT2, on = .(col1 = col1, col2 = col2), nomatch=0L]
– koteletje
Mar 7 at 6:52
2
@koteletje It's just the documented behavior with strings inon=
(from?data.table
)
– Frank
Mar 7 at 15:16
Happy to help @koteletje. We all need to be checking staff. Regarding your question of why it is two equals: besides being stated like that on thedata.table
documentation, the standard way to check if an object/something is exactly equal than another object/something is using logical operators, and it is stablished as two equals . Check this link for further understanding of it: statmethods.net/management/operators.html
– Carles Sans Fuentes
Mar 7 at 16:32
add a comment |
I think you just need to put two ==, as following:
DT1[DT2,
on=c(paste0(joinFields, '==', joinFields)),
nomatch=0L]
# col1 col2 lett
# 1: 1 5 a
# 2: 2 4 b
# 3: 3 3 c
# 4: 2 4 b
# 5: 3 3 c
# 6: 4 2 d
# 7: 5 1 e
# 8: 1 5 a
Even you do not need to use c()
:
DT1[DT2,
on=paste0(joinFields, '==', joinFields),
nomatch=0L]
# col1 col2 lett
# 1: 1 5 a
# 2: 2 4 b
# 3: 3 3 c
# 4: 2 4 b
# 5: 3 3 c
# 6: 4 2 d
# 7: 5 1 e
# 8: 1 5 a
I think you just need to put two ==, as following:
DT1[DT2,
on=c(paste0(joinFields, '==', joinFields)),
nomatch=0L]
# col1 col2 lett
# 1: 1 5 a
# 2: 2 4 b
# 3: 3 3 c
# 4: 2 4 b
# 5: 3 3 c
# 6: 4 2 d
# 7: 5 1 e
# 8: 1 5 a
Even you do not need to use c()
:
DT1[DT2,
on=paste0(joinFields, '==', joinFields),
nomatch=0L]
# col1 col2 lett
# 1: 1 5 a
# 2: 2 4 b
# 3: 3 3 c
# 4: 2 4 b
# 5: 3 3 c
# 6: 4 2 d
# 7: 5 1 e
# 8: 1 5 a
edited Mar 6 at 17:01
JonnyCrunch
1,658522
1,658522
answered Mar 6 at 16:42
Carles Sans FuentesCarles Sans Fuentes
612110
612110
Thank you very much @Carles Sans Fuentes. I must say I have some things to look into, because I don't understand why it works with the==
operator. I generally join like thisDT1[DT2, on = .(col1 = col1, col2 = col2), nomatch=0L]
– koteletje
Mar 7 at 6:52
2
@koteletje It's just the documented behavior with strings inon=
(from?data.table
)
– Frank
Mar 7 at 15:16
Happy to help @koteletje. We all need to be checking staff. Regarding your question of why it is two equals: besides being stated like that on thedata.table
documentation, the standard way to check if an object/something is exactly equal than another object/something is using logical operators, and it is stablished as two equals . Check this link for further understanding of it: statmethods.net/management/operators.html
– Carles Sans Fuentes
Mar 7 at 16:32
add a comment |
Thank you very much @Carles Sans Fuentes. I must say I have some things to look into, because I don't understand why it works with the==
operator. I generally join like thisDT1[DT2, on = .(col1 = col1, col2 = col2), nomatch=0L]
– koteletje
Mar 7 at 6:52
2
@koteletje It's just the documented behavior with strings inon=
(from?data.table
)
– Frank
Mar 7 at 15:16
Happy to help @koteletje. We all need to be checking staff. Regarding your question of why it is two equals: besides being stated like that on thedata.table
documentation, the standard way to check if an object/something is exactly equal than another object/something is using logical operators, and it is stablished as two equals . Check this link for further understanding of it: statmethods.net/management/operators.html
– Carles Sans Fuentes
Mar 7 at 16:32
Thank you very much @Carles Sans Fuentes. I must say I have some things to look into, because I don't understand why it works with the
==
operator. I generally join like this DT1[DT2, on = .(col1 = col1, col2 = col2), nomatch=0L]
– koteletje
Mar 7 at 6:52
Thank you very much @Carles Sans Fuentes. I must say I have some things to look into, because I don't understand why it works with the
==
operator. I generally join like this DT1[DT2, on = .(col1 = col1, col2 = col2), nomatch=0L]
– koteletje
Mar 7 at 6:52
2
2
@koteletje It's just the documented behavior with strings in
on=
(from ?data.table
)– Frank
Mar 7 at 15:16
@koteletje It's just the documented behavior with strings in
on=
(from ?data.table
)– Frank
Mar 7 at 15:16
Happy to help @koteletje. We all need to be checking staff. Regarding your question of why it is two equals: besides being stated like that on the
data.table
documentation, the standard way to check if an object/something is exactly equal than another object/something is using logical operators, and it is stablished as two equals . Check this link for further understanding of it: statmethods.net/management/operators.html– Carles Sans Fuentes
Mar 7 at 16:32
Happy to help @koteletje. We all need to be checking staff. Regarding your question of why it is two equals: besides being stated like that on the
data.table
documentation, the standard way to check if an object/something is exactly equal than another object/something is using logical operators, and it is stablished as two equals . Check this link for further understanding of it: statmethods.net/management/operators.html– Carles Sans Fuentes
Mar 7 at 16:32
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%2f55027851%2fjoin-datatables-using-more-than-one-column-name-stored-in-one-variable%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
2
You mention "update join" but don't use
:=
. Could you show and explain the desired output? Btw, you might need"=="
not"="
there, I guess, egDT1[DT2, on=sprintf("%s==%s", joinFields, joinFields)]
– Frank
Mar 6 at 16:40
2
the keys used to join appears to be the same in both tables so why not
DT1[DT2, on=joinFields, nomatch=0L]
?– chinsoon12
Mar 7 at 0:47
1
@Frank, thanks for your suggestion. It is indeed not an update join. I was a bit lazy and copied from the other thread. I should have added
let := i.lett
– koteletje
Mar 7 at 14:01
@chinsoon12, thanks that is a very short solution
– koteletje
Mar 7 at 14:01