0

我有以下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 
4

1 回答 1

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),])
于 2013-10-05T19:56:34.587 回答