I have 3 data.frames
> head(ON1)
Entrez.ID Nearest.Refseq Gene.Name Tag.Count
1 11302 NM_007377 Aatk 137.48
2 11303 NM_013454 Abca1 118.09
3 11305 NM_007379 Abca2 93.56
4 11306 NM_009592 Abcb7 92.42
5 11308 NM_007380 Abi1 410.73
6 11356 NM_009598 Abl5 149.46
> head(ON2)
Entrez.ID Nearest.Refseq Gene.Name Tag.Count
1 11303 NM_013454 Abca1 86.02
2 11305 NM_007379 Abca2 103.45
3 11306 NM_009592 Abcb7 95.32
4 11308 NM_007380 Abi1 313.85
5 11350 NM_009594 Abl1 116.24
6 11352 NM_009595 Abl2 155.76
> head(ON3)
Entrez.ID Nearest.Refseq Gene.Name Tag.Count
1 11303 NM_013454 Abca1 69.49
2 11305 NM_007379 Abca2 82.02
3 11306 NM_009592 Abcb7 83.16
4 11308 NM_007380 Abi1 306.44
5 11350 NM_009594 Abl1 150.37
6 11355 NM_009599 Abl4 154.93
Some rows are unique to only 1 data.frame (e.g. row1
of ON1
), some are common in 2 data.frames (e.g. row5
of ON2
and ON3
, this row doesn't exist in ON1
) and some are common in all data.frames (e.g. row2
of ON1
which is row1
in ON2
and ON3
). The only difference is the values in last column Tag.Count
I want to merge all 3 data.frames in such a way that I obtain only those rows in my final data.frame which are common among at-least 2 data.frames and the value of Tag.Count
which is higher among them will be assigned to that row.
> head(F)
Entrez.ID Nearest.Refseq Gene.Name Tag.Count
1 11303 NM_013454 Abca1 118.09
2 11305 NM_007379 Abca2 103.45
3 11306 NM_009592 Abcb7 95.32
4 11308 NM_007380 Abi1 410.73
5 11350 NM_009594 Abl1 150.37
Here you see that rows with Entrez.ID
= 11302
is removed as it only appeard once among all the data.frames and those rows which were common in at least 2 data.frames are appearing here but the Tag.Count
score which was maximum among all the data.frames is assigned to that row.
UPDATE
How to take average of rows. After merging above three datasets, instead of just keeping one row which has maximum Tag.count
value, I want to sum the Tag.count
value and divide by total number of rows having the same Entrez.ID
. In fact the values in the first 3 columns are the same, difference only arises in last column.
Example:
> head(d)
Entrez.ID Nearest.Refseq Gene.Name Tag.Count
1 11302 NM_007377 Aatk 137.48
2 11303 NM_013454 Abca1 118.09
7886 11303 NM_013454 Abca1 86.02
15407 11303 NM_013454 Abca1 69.49
3 11305 NM_007379 Abca2 93.56
7887 11305 NM_007379 Abca2 103.45
So, in this case as 3 rows have Entrez.ID
= 11303, the Tag.count
values will be summed up (118.09 + 86.02 + 69.49) and divided by 3 and final output will contain only 1 row having Entrez.ID
11303 and Tag.Count value
= Sum/no.of Rows