用词语很难解释,所以我会举一个具体的例子。
我的数据框如下所示:
V1 V2 V3 V4 V5 V6 V7 V8 V9
1 a b c d 0 0 0 0
2 e f g h 0 0 0 0
3 i j k l 0 0 0 0
. . . . . 0 0 0 0 (and so on)
0 0 0 0 0 1 a g c
0 0 0 0 0 1 a h c
0 0 0 0 0 . . . . (and so on)
0 0 0 0 0 2 u i l
0 0 0 0 0 2 k m n
0 0 0 0 0 . . . . (and so on)
0 0 0 0 0 3 u j v
0 0 0 0 0 3 z a r
0 0 0 0 0 . . . . (and so on)
我想要的是:
V1 V2 V3 V4 V5 V6 V7 V8 V9
1 a b c d 0 0 0 0
0 0 0 0 0 1 a g c
0 0 0 0 0 1 a h c
0 0 0 0 0 . . . . (and so on)
2 e f g h 0 0 0 0
0 0 0 0 0 2 u i l
0 0 0 0 0 2 k m n
0 0 0 0 0 . . . . (and so on)
3 i j k l 0 0 0 0
0 0 0 0 0 3 u j v
0 0 0 0 0 3 z a r
0 0 0 0 0 . . . . (and so on)
注意:unique(df$V1) = unique(df$V6)
答案 0 :(得分:2)
这是一个选项:
df[order(pmax(df$V1, df$V6)),]
# V1 V2 V3 V4 V5 V6 V7 V8 V9
# 1 1 a b c d 0 0 0 0
# 4 0 0 0 0 0 1 a g c
# 5 0 0 0 0 0 1 a h c
# 2 2 e f g h 0 0 0 0
# 6 0 0 0 0 0 2 u i l
# 7 0 0 0 0 0 2 k m n
# 3 3 i j k l 0 0 0 0
# 8 0 0 0 0 0 3 u j v
# 9 0 0 0 0 0 3 z a r