1
$\begingroup$

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.

$\endgroup$
11
  • $\begingroup$ What do you mean 'it didn't work'? $\endgroup$
    – user438383
    Apr 15 at 9:56
  • $\begingroup$ 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 statmethods.net/management/merging.html $\endgroup$
    – user438383
    Apr 15 at 10:00
  • $\begingroup$ I just use R showing the file content but wanted to do using unix command or R command. $\endgroup$ Apr 15 at 10:34
  • 1
    $\begingroup$ it doesn't need to be sorted $\endgroup$
    – user438383
    Apr 15 at 11:15
  • 1
    $\begingroup$ 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. $\endgroup$ Apr 15 at 11:20
2
$\begingroup$

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").

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.