简体   繁体   中英

Order a Matrix by the Values in One Column

I have a data set called regional_regression , as below.

regional_regression

$`East Asia & Pacific (all income levels)`
     (Intercept)       gdpcap 
48.180966940 -0.001276384 

$`Europe & Central Asia (all income levels)`
  (Intercept)        gdpcap 
28.8153087170 -0.0005021196 

$`Latin America & Caribbean (all income levels)`
 (Intercept)       gdpcap 
59.706105856 -0.003207999 

$`Middle East & North Africa (all income levels)`
  (Intercept)        gdpcap 
54.6225615193 -0.0008088255 

$`North America`
 (Intercept)       gdpcap 
32.444196028 -0.000599279 

$`South Asia`
 (Intercept)       gdpcap 
109.71477941  -0.02396868 

$`Sub-Saharan Africa (all income levels)`
  (Intercept)        gdpcap 
105.434754813  -0.006815668 

I want to get this data in an orderly table, and sort by the column that will be called "Coefficient".

regional_regressional <- regional_regression %>% as.data.frame() 
regional_regression_table <- t(regional_regressional)
col_names <- c("Intercept", "Coefficient") 
colnames(regional_regression_table) <- col_names

Since it seems that I am dealing with a matrix, I keep getting errors when I've tried things like:

regional_regression_table[order(regional_regression_table$Coefficient),]

regional_regression_table[order(regional_regression_table[,"V2"]),]

setorder(regional_regression_table, -Coefficient)

Any insight is much appreciated!

If Coefficient is your second column in matrix then you just do the following. Since it is a matrix , you order it using column number.

 regional_regression_table[order(regional_regression_table[,2]),]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM