我有两个简单的矩阵(或df)要合并:
a <- cbind(one=0:15, two=0:15, three=0:15)
b <- cbind(one=0:15, two=0:15, three=0:15)
#a <- data.frame(one=0:15, two=0:15, three=0:15)
#b <- data.frame(one=0:15, two=0:15, three=0:15)
没问题:在对第一列进行排序后,第一列的输出从 0 到 15 递增:
merge(a,b,by=c("one"), sort=T)
one two.x three.x two.y three.y
1 0 0 0 0 0
2 1 1 1 1 1
3 2 2 2 2 2
4 3 3 3 3 3
5 4 4 4 4 4
6 5 5 5 5 5
7 6 6 6 6 6
8 7 7 7 7 7
9 8 8 8 8 8
10 9 9 9 9 9
11 10 10 10 10 10
12 11 11 11 11 11
13 12 12 12 12 12
14 13 13 13 13 13
15 14 14 14 14 14
16 15 15 15 15 15
但是等等:当合并两列时——都是数字——排序顺序突然看起来像字母。
merge(a,b,by=c("one", "two"), sort=T)
one two three.x three.y
1 0 0 0 0
2 1 1 1 1
3 10 10 10 10
4 11 11 11 11
5 12 12 12 12
6 13 13 13 13
7 14 14 14 14
8 15 15 15 15
9 2 2 2 2
10 3 3 3 3
11 4 4 4 4
12 5 5 5 5
13 6 6 6 6
14 7 7 7 7
15 8 8 8 8
16 9 9 9 9
额,恶心。这是怎么回事?我该怎么办?