R:rowsum 函数在聚合后更改组的顺序

R: rowsum function changes order of groups after aggregation

我有这个数据框有重复项(相同 ID 但数字不同):

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

我想对具有相同 ID 的向量求和,所以我使用了 rowsum:

m <- rowsum(m, m$ID)

但是它弄乱了行的顺序,显示如下:

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

而不是我想要的:

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

有人知道如何解决这个问题吗?

reorder = FALSE放入rowsum


来自?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.