<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>

CC BY-SA 4.0
2021-04-16T22:00:56.770Z
2021-04-16T22:00:56.770Z
Phoenix Mu
<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>

CC BY-SA 4.0
2021-04-15T09:56:05.320Z
What do you mean 'it didn't work'?
user438383
CC BY-SA 4.0
2021-04-15T10:00:24.450Z
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
user438383
CC BY-SA 4.0
2021-04-15T10:34:30.427Z
I just use R showing the file content but wanted to do using unix command or R command.
bioinfonext
CC BY-SA 4.0
2021-04-15T10:43:45.633Z
OK - it's better to try in R IMO. Did you try follow the link I posted?
user438383
CC BY-SA 4.0
2021-04-15T10:49:18.907Z
Error while doing in R, updated the command and error in the post.
bioinfonext
CC BY-SA 4.0
2021-04-15T10:51:49.580Z
Do we need to sort file in R before merging?
bioinfonext
CC BY-SA 4.0
2021-04-15T10:57:48.300Z
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?
bioinfonext
CC BY-SA 4.0
2021-04-15T11:03:47.313Z
no issue now while merging but when trying to sort file it shows error;
bioinfonext
CC BY-SA 4.0
2021-04-15T11:15:30.840Z
it doesn't need to be sorted
user438383
CC BY-SA 4.0
2021-04-15T11:20:10.647Z
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.
bioinfonext
CC BY-SA 4.0
2021-04-16T10:39:56.827Z
@user438383 or bioinfonext could one of you please post an answer with the solution you found so this question can be marked as answered?
terdon
<r><bash><linux>
how to sort and join two files based on First Column Id
# 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**: @user438383 or bioinfonext 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")`.


---

