Order dataframe by column of another dataframe












0














Consider the following df: df1 <-data.frame('GID'=c('GID1','GID3','GID2','GID1','GID2'), 'Sequence'= c(4,7,6,2,3)) df2 <- data.frame('GID' = c('GID3','GID1','GID2','GID1','GID2'),'Trial'=c('SA1','SA5','ES4','ES3','ES9'))



I want to order df1 by the column df2$GID such that I could cbind the df2$Trial column into df1. I know match can do that but match only finds the first occurrence of values and I have repeated values. Thus I'm confused on the way to do that as this is a subset of a large data.frame.










share|improve this question






















  • You have duplicates in both datasets for column 'GID' May need to create a sequence column
    – akrun
    Nov 23 '18 at 16:39










  • create a sequence column for df2 and then ?
    – Alexandre Mondaini
    Nov 23 '18 at 16:46
















0














Consider the following df: df1 <-data.frame('GID'=c('GID1','GID3','GID2','GID1','GID2'), 'Sequence'= c(4,7,6,2,3)) df2 <- data.frame('GID' = c('GID3','GID1','GID2','GID1','GID2'),'Trial'=c('SA1','SA5','ES4','ES3','ES9'))



I want to order df1 by the column df2$GID such that I could cbind the df2$Trial column into df1. I know match can do that but match only finds the first occurrence of values and I have repeated values. Thus I'm confused on the way to do that as this is a subset of a large data.frame.










share|improve this question






















  • You have duplicates in both datasets for column 'GID' May need to create a sequence column
    – akrun
    Nov 23 '18 at 16:39










  • create a sequence column for df2 and then ?
    – Alexandre Mondaini
    Nov 23 '18 at 16:46














0












0








0







Consider the following df: df1 <-data.frame('GID'=c('GID1','GID3','GID2','GID1','GID2'), 'Sequence'= c(4,7,6,2,3)) df2 <- data.frame('GID' = c('GID3','GID1','GID2','GID1','GID2'),'Trial'=c('SA1','SA5','ES4','ES3','ES9'))



I want to order df1 by the column df2$GID such that I could cbind the df2$Trial column into df1. I know match can do that but match only finds the first occurrence of values and I have repeated values. Thus I'm confused on the way to do that as this is a subset of a large data.frame.










share|improve this question













Consider the following df: df1 <-data.frame('GID'=c('GID1','GID3','GID2','GID1','GID2'), 'Sequence'= c(4,7,6,2,3)) df2 <- data.frame('GID' = c('GID3','GID1','GID2','GID1','GID2'),'Trial'=c('SA1','SA5','ES4','ES3','ES9'))



I want to order df1 by the column df2$GID such that I could cbind the df2$Trial column into df1. I know match can do that but match only finds the first occurrence of values and I have repeated values. Thus I'm confused on the way to do that as this is a subset of a large data.frame.







r






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 23 '18 at 16:37









Alexandre Mondaini

986




986












  • You have duplicates in both datasets for column 'GID' May need to create a sequence column
    – akrun
    Nov 23 '18 at 16:39










  • create a sequence column for df2 and then ?
    – Alexandre Mondaini
    Nov 23 '18 at 16:46


















  • You have duplicates in both datasets for column 'GID' May need to create a sequence column
    – akrun
    Nov 23 '18 at 16:39










  • create a sequence column for df2 and then ?
    – Alexandre Mondaini
    Nov 23 '18 at 16:46
















You have duplicates in both datasets for column 'GID' May need to create a sequence column
– akrun
Nov 23 '18 at 16:39




You have duplicates in both datasets for column 'GID' May need to create a sequence column
– akrun
Nov 23 '18 at 16:39












create a sequence column for df2 and then ?
– Alexandre Mondaini
Nov 23 '18 at 16:46




create a sequence column for df2 and then ?
– Alexandre Mondaini
Nov 23 '18 at 16:46












1 Answer
1






active

oldest

votes


















1














Have you tried using match this way: df1 <- df1[match(df2$GID, df1$GID),] (make sure to review the results)?



 GID Sequence
2 GID3 7
1 GID1 4
3 GID2 6
1.1 GID1 4
3.1 GID2 6


Do you want to get the cartesian product of GID, Sequence and Trial?



Also, could there be any GID of df2 that is not present in df1 and how would you like to handle that?



Do you have any data acting as a "key", identifying uniquely each entry?






share|improve this answer























  • No, I don't need the cartesian product.
    – Alexandre Mondaini
    Nov 23 '18 at 17:19










  • No, I don't need the cartesian product. the df1 was first shorter than df2. df1 had no repeated values while df2 had. Then I expanded df1 as follows df1[df2$GID,]. Thus all GID from df2 are present now in df1 and replicated by df2. What identifies uniquely each GID is the combination of the two columns of df2 e.g: df2$GID,df2$Trial. Such combination is unique and is in fact what I want to transfer to df1.
    – Alexandre Mondaini
    Nov 23 '18 at 17:25










  • Actually, df1, does not have each value of GID matched to a unique value of Sequence, thus I can't understand how it doesn't have or shouldn't have replicates.
    – LostIT
    Nov 23 '18 at 17:44












  • actually is the GID column of df2 that has a unique Trial. So df2 dataframe has duplicated GIDs but unique Trial numbers for the duplicated GIDs . Do you understand what I mean ?
    – Alexandre Mondaini
    Nov 23 '18 at 18:00










  • This can't be true, since e.g. GID1 is matched to both SA5 and ES3 trials. Do you mean that the "key" of df2, is the Trial and a single Trial value can only be matched to one GID value?
    – LostIT
    Nov 23 '18 at 18:06













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%2f53450253%2forder-dataframe-by-column-of-another-dataframe%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









1














Have you tried using match this way: df1 <- df1[match(df2$GID, df1$GID),] (make sure to review the results)?



 GID Sequence
2 GID3 7
1 GID1 4
3 GID2 6
1.1 GID1 4
3.1 GID2 6


Do you want to get the cartesian product of GID, Sequence and Trial?



Also, could there be any GID of df2 that is not present in df1 and how would you like to handle that?



Do you have any data acting as a "key", identifying uniquely each entry?






share|improve this answer























  • No, I don't need the cartesian product.
    – Alexandre Mondaini
    Nov 23 '18 at 17:19










  • No, I don't need the cartesian product. the df1 was first shorter than df2. df1 had no repeated values while df2 had. Then I expanded df1 as follows df1[df2$GID,]. Thus all GID from df2 are present now in df1 and replicated by df2. What identifies uniquely each GID is the combination of the two columns of df2 e.g: df2$GID,df2$Trial. Such combination is unique and is in fact what I want to transfer to df1.
    – Alexandre Mondaini
    Nov 23 '18 at 17:25










  • Actually, df1, does not have each value of GID matched to a unique value of Sequence, thus I can't understand how it doesn't have or shouldn't have replicates.
    – LostIT
    Nov 23 '18 at 17:44












  • actually is the GID column of df2 that has a unique Trial. So df2 dataframe has duplicated GIDs but unique Trial numbers for the duplicated GIDs . Do you understand what I mean ?
    – Alexandre Mondaini
    Nov 23 '18 at 18:00










  • This can't be true, since e.g. GID1 is matched to both SA5 and ES3 trials. Do you mean that the "key" of df2, is the Trial and a single Trial value can only be matched to one GID value?
    – LostIT
    Nov 23 '18 at 18:06


















1














Have you tried using match this way: df1 <- df1[match(df2$GID, df1$GID),] (make sure to review the results)?



 GID Sequence
2 GID3 7
1 GID1 4
3 GID2 6
1.1 GID1 4
3.1 GID2 6


Do you want to get the cartesian product of GID, Sequence and Trial?



Also, could there be any GID of df2 that is not present in df1 and how would you like to handle that?



Do you have any data acting as a "key", identifying uniquely each entry?






share|improve this answer























  • No, I don't need the cartesian product.
    – Alexandre Mondaini
    Nov 23 '18 at 17:19










  • No, I don't need the cartesian product. the df1 was first shorter than df2. df1 had no repeated values while df2 had. Then I expanded df1 as follows df1[df2$GID,]. Thus all GID from df2 are present now in df1 and replicated by df2. What identifies uniquely each GID is the combination of the two columns of df2 e.g: df2$GID,df2$Trial. Such combination is unique and is in fact what I want to transfer to df1.
    – Alexandre Mondaini
    Nov 23 '18 at 17:25










  • Actually, df1, does not have each value of GID matched to a unique value of Sequence, thus I can't understand how it doesn't have or shouldn't have replicates.
    – LostIT
    Nov 23 '18 at 17:44












  • actually is the GID column of df2 that has a unique Trial. So df2 dataframe has duplicated GIDs but unique Trial numbers for the duplicated GIDs . Do you understand what I mean ?
    – Alexandre Mondaini
    Nov 23 '18 at 18:00










  • This can't be true, since e.g. GID1 is matched to both SA5 and ES3 trials. Do you mean that the "key" of df2, is the Trial and a single Trial value can only be matched to one GID value?
    – LostIT
    Nov 23 '18 at 18:06
















1












1








1






Have you tried using match this way: df1 <- df1[match(df2$GID, df1$GID),] (make sure to review the results)?



 GID Sequence
2 GID3 7
1 GID1 4
3 GID2 6
1.1 GID1 4
3.1 GID2 6


Do you want to get the cartesian product of GID, Sequence and Trial?



Also, could there be any GID of df2 that is not present in df1 and how would you like to handle that?



Do you have any data acting as a "key", identifying uniquely each entry?






share|improve this answer














Have you tried using match this way: df1 <- df1[match(df2$GID, df1$GID),] (make sure to review the results)?



 GID Sequence
2 GID3 7
1 GID1 4
3 GID2 6
1.1 GID1 4
3.1 GID2 6


Do you want to get the cartesian product of GID, Sequence and Trial?



Also, could there be any GID of df2 that is not present in df1 and how would you like to handle that?



Do you have any data acting as a "key", identifying uniquely each entry?







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 23 '18 at 17:01

























answered Nov 23 '18 at 16:47









LostIT

8318




8318












  • No, I don't need the cartesian product.
    – Alexandre Mondaini
    Nov 23 '18 at 17:19










  • No, I don't need the cartesian product. the df1 was first shorter than df2. df1 had no repeated values while df2 had. Then I expanded df1 as follows df1[df2$GID,]. Thus all GID from df2 are present now in df1 and replicated by df2. What identifies uniquely each GID is the combination of the two columns of df2 e.g: df2$GID,df2$Trial. Such combination is unique and is in fact what I want to transfer to df1.
    – Alexandre Mondaini
    Nov 23 '18 at 17:25










  • Actually, df1, does not have each value of GID matched to a unique value of Sequence, thus I can't understand how it doesn't have or shouldn't have replicates.
    – LostIT
    Nov 23 '18 at 17:44












  • actually is the GID column of df2 that has a unique Trial. So df2 dataframe has duplicated GIDs but unique Trial numbers for the duplicated GIDs . Do you understand what I mean ?
    – Alexandre Mondaini
    Nov 23 '18 at 18:00










  • This can't be true, since e.g. GID1 is matched to both SA5 and ES3 trials. Do you mean that the "key" of df2, is the Trial and a single Trial value can only be matched to one GID value?
    – LostIT
    Nov 23 '18 at 18:06




















  • No, I don't need the cartesian product.
    – Alexandre Mondaini
    Nov 23 '18 at 17:19










  • No, I don't need the cartesian product. the df1 was first shorter than df2. df1 had no repeated values while df2 had. Then I expanded df1 as follows df1[df2$GID,]. Thus all GID from df2 are present now in df1 and replicated by df2. What identifies uniquely each GID is the combination of the two columns of df2 e.g: df2$GID,df2$Trial. Such combination is unique and is in fact what I want to transfer to df1.
    – Alexandre Mondaini
    Nov 23 '18 at 17:25










  • Actually, df1, does not have each value of GID matched to a unique value of Sequence, thus I can't understand how it doesn't have or shouldn't have replicates.
    – LostIT
    Nov 23 '18 at 17:44












  • actually is the GID column of df2 that has a unique Trial. So df2 dataframe has duplicated GIDs but unique Trial numbers for the duplicated GIDs . Do you understand what I mean ?
    – Alexandre Mondaini
    Nov 23 '18 at 18:00










  • This can't be true, since e.g. GID1 is matched to both SA5 and ES3 trials. Do you mean that the "key" of df2, is the Trial and a single Trial value can only be matched to one GID value?
    – LostIT
    Nov 23 '18 at 18:06


















No, I don't need the cartesian product.
– Alexandre Mondaini
Nov 23 '18 at 17:19




No, I don't need the cartesian product.
– Alexandre Mondaini
Nov 23 '18 at 17:19












No, I don't need the cartesian product. the df1 was first shorter than df2. df1 had no repeated values while df2 had. Then I expanded df1 as follows df1[df2$GID,]. Thus all GID from df2 are present now in df1 and replicated by df2. What identifies uniquely each GID is the combination of the two columns of df2 e.g: df2$GID,df2$Trial. Such combination is unique and is in fact what I want to transfer to df1.
– Alexandre Mondaini
Nov 23 '18 at 17:25




No, I don't need the cartesian product. the df1 was first shorter than df2. df1 had no repeated values while df2 had. Then I expanded df1 as follows df1[df2$GID,]. Thus all GID from df2 are present now in df1 and replicated by df2. What identifies uniquely each GID is the combination of the two columns of df2 e.g: df2$GID,df2$Trial. Such combination is unique and is in fact what I want to transfer to df1.
– Alexandre Mondaini
Nov 23 '18 at 17:25












Actually, df1, does not have each value of GID matched to a unique value of Sequence, thus I can't understand how it doesn't have or shouldn't have replicates.
– LostIT
Nov 23 '18 at 17:44






Actually, df1, does not have each value of GID matched to a unique value of Sequence, thus I can't understand how it doesn't have or shouldn't have replicates.
– LostIT
Nov 23 '18 at 17:44














actually is the GID column of df2 that has a unique Trial. So df2 dataframe has duplicated GIDs but unique Trial numbers for the duplicated GIDs . Do you understand what I mean ?
– Alexandre Mondaini
Nov 23 '18 at 18:00




actually is the GID column of df2 that has a unique Trial. So df2 dataframe has duplicated GIDs but unique Trial numbers for the duplicated GIDs . Do you understand what I mean ?
– Alexandre Mondaini
Nov 23 '18 at 18:00












This can't be true, since e.g. GID1 is matched to both SA5 and ES3 trials. Do you mean that the "key" of df2, is the Trial and a single Trial value can only be matched to one GID value?
– LostIT
Nov 23 '18 at 18:06






This can't be true, since e.g. GID1 is matched to both SA5 and ES3 trials. Do you mean that the "key" of df2, is the Trial and a single Trial value can only be matched to one GID value?
– LostIT
Nov 23 '18 at 18:06




















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.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • 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%2f53450253%2forder-dataframe-by-column-of-another-dataframe%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

Contact image not getting when fetch all contact list from iPhone by CNContact

count number of partitions of a set with n elements into k subsets

A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks