-4

I would like to get the sum of duplicates. For example,

No.   MRN   Bicycle   
1.    010      1
2.    011      1
3.    011      1
4.    012      0
5.    013      1 
6.    014      0
7.    015      1
8.    015      1
9.    015      1

1 is yes 
0 is no

May I know r code that MRN 011 has 1 bicycle. NOT 2.(MRN 015 has 1 not 3) And I would like to know total number of bicycles answer as 4). NOT 7. Please show me how to put command in R. Thanks.

Myint Win
  • 1
  • 2

1 Answers1

0

We can use table to find the 'MRN's that have duplicates.

+(table(df1[-1])>1)[,2]
# 10 11 12 13 
# 0  1  0  0 

If we need the total sum, just wrap it with sum

sum(table(df1[-1])>1)
#[1] 1

If the intention is to find whether there are any 'Bicycle' in each of the 'MRN'

+(table(df1[-1])>0)[,2]
#10 11 12 13 
#1  1  0  1 

and its sum is 3.

akrun
  • 874,273
  • 37
  • 540
  • 662