我有以下data.frame
更正件
A B C D
1 1.00000000 0.736315370 0.518221125 0.711950770
2 0.73631537 1.000000000 0.674467201 0.867328184
3 0.51822112 0.674467201 1.000000000 0.688871658
4 0.71195077 0.867328184 0.688871658 1.000000000
和第二个data.frame
data cluster_id sector_code
1 1 A
2 1 C
3 2 B
4 3 D
我希望数据按照corr data.frame
所以输出最终应该是这样的:
data cluster_id sector_code
1 1 A
2 2 B
3 1 C
4 3 D
答案 0 :(得分:0)
在我看来,您希望按字母顺序重新排列数据。 这是我用的。
假设您的第二个data.frame
被称为“D”
D <- data.frame(c(1,2,3,4), c(1,1,2,3), c("A","C","B","D"))
D <- data.frame(c(1,2,3,4), c(1,1,2,3), c("A","C","B","D"))
colnames(D) <- c("data","cluster_id","sector_code")
D <- with(D, D[order(D$sector_code, D$cluster_id), ])
我希望这就是你要找的东西。如果不是,你也可以使用它:
corr <- data.frame(c(1,0.73631537,0.51822112,0.71195077),c(0.736315370,1,0.51822112, 0.867328184),c(0.518221125,0.674467201,1,0.688871658)
,c(0.711950770,0.867328184,0.688871658,1))
colnames(corr) <- c("A","B","C","D")
D <- with(D, D[order(colnames(corr), D$sector_code),])