Answers.Body: <p>You should check <code>left_join()</code> method in <code>dplyr</code> package. In this way you do not need to sort the data frames, as the function automatically take care of this.
  For example: <code>merged_df &lt;- left_join(otu, taxa, by = &quot;ID&quot;)</code>.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-04-16T22:00:56.770Z
Answers.LastActivityDate: 2021-04-16T22:00:56.770Z
Answers.UserDisplayName: Phoenix Mu
Body: <p>I have got two files large files like this in Tab-delimited format, trying to merge in R, do we need to sort the file before merging?</p>
  <pre><code> otu &lt;- read.table(&quot;asv.txt&quot;, sep=&quot;\t&quot;, header=T, blank.lines.skip=F, check.names=F)
  &gt; head(otu)
                  ID                   Soil.9.S42 Soil-27.S33.L001 Soil-45.S54.L001
  1 d2ec9f3b77975c0f457e4b7413b217ff         34               51               84
  2 3147790f0d5a78316fb9dd64f53b9473         28               32               52
  3 97aecc1f35cc1f50db507ad71dd22367         33                0                7
  4 bfad6370d28182cc6304844e9bec7fb6        545              237              127
  5 5fa2a987221a1d9ca416148570c18086          0                0                0
  6 8e60d301122d7aa359eb6b0b00f37f62        124              143              128
  
  
  &gt; taxa &lt;- read.table(&quot;taxonomy.txt&quot;, sep=&quot;\t&quot;, header=F ,stringsAsFactors=F,quote=&quot;&quot;)
  &gt; head(taxa)
                    ID                     V2             V3                  V4
  d2ec9f3b77975c0f457e4b7413b217ff Bacteria  Cyanobacteria    Oxyphotobacteria
  3147790f0d5a78316fb9dd64f53b9473 Bacteria Proteobacteria Alphaproteobacteria
  97aecc1f35cc1f50db507ad71dd22367 Bacteria Proteobacteria Gammaproteobacteria
  bfad6370d28182cc6304844e9bec7fb6 Bacteria Proteobacteria Alphaproteobacteria
  5fa2a987221a1d9ca416148570c18086 Bacteria Proteobacteria Alphaproteobacteria
  8e60d301122d7aa359eb6b0b00f37f62 Bacteria Proteobacteria Deltaproteobacteria
  </code></pre>
  <p>Now having issue with sorting file but merge command work perfectly;</p>
  <pre><code>newotu &lt;- otu[order(ID),]
  Error in order(ID) : object 'ID' not found
  &gt; total &lt;- merge(otu,taxa,by=&quot;ID&quot;)
  </code></pre>
  <p>Thanks for your help.</p>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2021-04-15T09:56:05.320Z
Comments.Text: What do you mean 'it didn't work'?
Comments.UserDisplayName: user438383
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2021-04-15T10:00:24.450Z
Comments.Text: Also I'm confused - you seem to be reading the files into R and then using unix commands to merge? I prefer to do this sort of things in R - this should help https://www.statmethods.net/management/merging.html
Comments.UserDisplayName: user438383
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2021-04-15T10:34:30.427Z
Comments.Text: I just use R showing the file content but wanted to do using unix command or R command.
Comments.UserDisplayName: bioinfonext
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2021-04-15T10:43:45.633Z
Comments.Text: OK - it's better to try in R IMO. Did you try follow the link I posted?
Comments.UserDisplayName: user438383
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2021-04-15T10:49:18.907Z
Comments.Text: Error while doing in R, updated the command and error in the post.
Comments.UserDisplayName: bioinfonext
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2021-04-15T10:51:49.580Z
Comments.Text: Do we need to sort file in R before merging?
Comments.UserDisplayName: bioinfonext
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2021-04-15T10:57:48.300Z
Comments.Text: There was some issue with the command like header True or False, now fix it and trying again, but do we need to sort the file in R before merging?
Comments.UserDisplayName: bioinfonext
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2021-04-15T11:03:47.313Z
Comments.Text: no issue now while merging but when trying to sort file it shows error;
Comments.UserDisplayName: bioinfonext
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2021-04-15T11:15:30.840Z
Comments.Text: it doesn't need to be sorted
Comments.UserDisplayName: user438383
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2021-04-15T11:20:10.647Z
Comments.Text: Thank you so much for your help. this is a very easy way to merge files in R. I also checked the file and it merges perfectly.
Comments.UserDisplayName: bioinfonext
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2021-04-16T10:39:56.827Z
Comments.Text: @user438383 or bioinfonext could one of you please post an answer with the solution you found so this question can be marked as answered?
Comments.UserDisplayName: terdon
Tags: <r><bash><linux>
Title: how to sort and join two files based on First Column Id
ThreadText: # how to sort and join two files based on First Column Id
  
  (Asked by: username_0 on 2021-04-15)
  
  I have got two files large files like this in Tab-delimited format, trying to merge in R, do we need to sort the file before merging?
  
  ```
   otu <- read.table("asv.txt", sep="\t", header=T, blank.lines.skip=F, check.names=F)
  > head(otu)
                  ID                   Soil.9.S42 Soil-27.S33.L001 Soil-45.S54.L001
  1 d2ec9f3b77975c0f457e4b7413b217ff         34               51               84
  2 3147790f0d5a78316fb9dd64f53b9473         28               32               52
  3 97aecc1f35cc1f50db507ad71dd22367         33                0                7
  4 bfad6370d28182cc6304844e9bec7fb6        545              237              127
  5 5fa2a987221a1d9ca416148570c18086          0                0                0
  6 8e60d301122d7aa359eb6b0b00f37f62        124              143              128
  
  > taxa <- read.table("taxonomy.txt", sep="\t", header=F ,stringsAsFactors=F,quote="")
  > head(taxa)
                    ID                     V2             V3                  V4
  d2ec9f3b77975c0f457e4b7413b217ff Bacteria  Cyanobacteria    Oxyphotobacteria
  3147790f0d5a78316fb9dd64f53b9473 Bacteria Proteobacteria Alphaproteobacteria
  97aecc1f35cc1f50db507ad71dd22367 Bacteria Proteobacteria Gammaproteobacteria
  bfad6370d28182cc6304844e9bec7fb6 Bacteria Proteobacteria Alphaproteobacteria
  5fa2a987221a1d9ca416148570c18086 Bacteria Proteobacteria Alphaproteobacteria
  8e60d301122d7aa359eb6b0b00f37f62 Bacteria Proteobacteria Deltaproteobacteria
  
  ```
  
  Now having issue with sorting file but merge command work perfectly;
  
  ```
  newotu <- otu[order(ID),]
  Error in order(ID) : object 'ID' not found
  > total <- merge(otu,taxa,by="ID")
  
  ```
  
  Thanks for your help.
  
  **Tags:** r, bash, linux
  
  > **Comments:**
  > **username_1**: What do you mean 'it didn't work'?  
  > **username_1**: Also I'm confused - you seem to be reading the files into R and then using unix commands to merge? I prefer to do this sort of things in R - this should help https://www.statmethods.net/management/merging.html  
  > **username_0**: I just use R showing the file content but wanted to do using unix command or R command.  
  > **username_1**: OK - it's better to try in R IMO. Did you try follow the link I posted?  
  > **username_0**: Error while doing in R, updated the command and error in the post.  
  > **username_0**: Do we need to sort file in R before merging?  
  > **username_0**: There was some issue with the command like header True or False, now fix it and trying again, but do we need to sort the file in R before merging?  
  > **username_0**: no issue now while merging but when trying to sort file it shows error;  
  > **username_1**: it doesn't need to be sorted  
  > **username_0**: Thank you so much for your help. this is a very easy way to merge files in R. I also checked the file and it merges perfectly.  
  > **username_2**: @username_1 or username_0 could one of you please post an answer with the solution you found so this question can be marked as answered?  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_3 on 2021-04-16. Score: 3 | Accepted answer
  
  You should check `left_join()` method in `dplyr` package. In this way you do not need to sort the data frames, as the function automatically take care of this.
  For example: `merged_df <- left_join(otu, taxa, by = "ID")`.
  
  
  ---
  
  