Merge data
You will often want to combine different datasets with a column in common. This is easy to do in R.
For example the mapping of sequencing reads of different tissues to contigs generated from all the reads.
It could also be different information on the same individuals.
In the sequencing example, the mapping datasets may not fully overlap. For example contig 1 may be represented in tissue 1, but not tissue 2, and hence dataset1 but not dataset2.
We would like to merge the datasets based on their common column (contig). The resulting dataset can be used for statistical analysis, for example for differences in the read number per tissue.
This is straightforward in R using the merge
command.
First load the datasets:
tissueData1<-read.table("/Users/username/Desktop/tissueData1", header=T)
str(tissueData1
tissueData2<-read.table("/Users/username/Desktop/tissueData2.txt", header=T)
str(tissueData2)
tissueData3<-read.table("/Users/username/Desktop/tissueData3.txt", header=T)
str(tissueData3)
tissueData4<-read.table("/Users/username/Desktop/tissueData4.txt", header=T)
str(tissueData4)
Now merge by adding one dataset at a time. We merge based on the total_contig
and tissue_contig
columns, which have overlapping values (i.e. contig1, contig2, etc).
merged1=merge(tissueData1, tissueData2, by.x="tissue1_contig", by.y="tissue2_contig", all=T )
merged2=merge(merged1, tissueData3, by.x="total_contig", by.y="tissue3_contig", all=T )
mergedData=merge(merged3, tissueData4, by.x="total_contig", by.y="tissue4_contig", all=T )
Note: all=T
retains the data from rows that did not have a common value in the columns used for merging. In this case NAs are automatically added to the values in the merged row.
The merge command can do more tricks, to learn more about it:
?merge