1

I've got this data frame which has duplicates (same ID but different numbers):

ID X1 X2 X3 X4 X5
45 1  0  0  1  0
45 0  1  0  0  1
15 1  0  1  0  0
7  1  0  1  1  0
7  0  1  0  0  0

I want to sum the vectors that have the same ID so I've used rowsum:

m <- rowsum(m, m$ID)

However it messes up with the order of the rows showing something like this:

ID X1 X2 X3 X4 X5
15 1  0  1  0  0
45 1  1  0  1  1
 7 1  1  1  1  0

Instead of what I want:

ID X1 X2 X3 X4 X5
45 1  1  0  1  1
15 1  0  1  0  0
 7 1  1  1  1  0

Anyone knows how to fix this?

Zheyuan Li
  • 71,365
  • 17
  • 180
  • 248
pw225
  • 51
  • 6

1 Answers1

2

Put reorder = FALSE in rowsum.


From ?rowsum:

reorder: if ‘TRUE’, then the result will be in order of
          ‘sort(unique(group))’, if ‘FALSE’, it will be in the order
          that groups were encountered.
Zheyuan Li
  • 71,365
  • 17
  • 180
  • 248