简体   繁体   English

如何在R中基于另一列排序一列

[英]How to order one column based on another in R

I have this data where I want to order val based on quant 1 will correspond to the highest value and so on.我有这些数据,我想根据 quant 1 订购 val 将对应于最高值,依此类推。

So 1 will correspond to 181.2349所以 1 将对应 181.2349

data = structure(list(quant = c(0, 0.02, 0.04, 0.06, 0.08, 0.1, 0.12, 
0.14, 0.16, 0.18, 0.2, 0.22, 0.24, 0.26, 0.28, 0.3, 0.32, 0.34, 
0.36, 0.38, 0.4, 0.42, 0.44, 0.46, 0.48, 0.5, 0.52, 0.54, 0.56, 
0.58, 0.6, 0.62, 0.64, 0.66, 0.68, 0.7, 0.72, 0.74, 0.76, 0.78, 
0.8, 0.82, 0.84, 0.86, 0.88, 0.9, 0.92, 0.94, 0.96, 0.98, 1), 
    val = c(47.91623, 90.3489408, 127.16448, 70.526045, 66.3226236, 
    85.103976, 139.317196, 127.446425999999, 91.5951164, 86.805257, 
    111.71706, 79.3636359999997, 73.1136444, 147.4201476, 65.2126171999996, 
    135.85975, 127.401408, 106.597378999999, 101.1695592, 94.1209831999999, 
    93.1355219999998, 96.3409336000001, 90.2044183999998, 75.7257826, 
    147.727516, 80.45166, 102.691942399999, 77.5738932, 62.665275199999, 
    128.7217, 156.20672, 132.990364, 118.481792, 118.512295599999, 
    57.3580020000001, 110.16883, 145.284928, 155.691106799999, 
    134.824147999999, 161.223344, 98.6174559999996, 99.0563548, 
    131.044792000001, 124.3800214, 99.4231451999992, 154.733724999998, 
    120.806394399999, 86.9254320000016, 139.611945600001, 181.234905600001, 
    119.7396)), row.names = c(NA, -51L), class = c("data.table", 
"data.frame"))

You can do:你可以做:

data[] <- lapply(data, sort, decreasing = TRUE)
head(data)
   quant      val
1:  1.00 181.2349
2:  0.98 161.2233
3:  0.96 156.2067
4:  0.94 155.6911
5:  0.92 154.7337
6:  0.90 147.7275

you can solve the problem as follows:您可以按如下方式解决问题:

data[, `:=`(quant=sort(quant, TRUE), val=sort(val, TRUE))]

 head(data)
   quant      val
1:  1.00 181.2349
2:  0.98 161.2233
3:  0.96 156.2067
4:  0.94 155.6911
5:  0.92 154.7337
6:  0.90 147.7275

# or 
cols = c("quant", "val")
data[, (cols) := lapply(.SD, sort, TRUE), .SDcols=cols]

dplyr option (updated thanks to @Adam): dplyr选项(感谢@Adam 更新):

library(dplyr)
data %>%
  mutate(across(everything(), sort, decreasing = TRUE))

Output:输出:

   quant       val
 1:  1.00 181.23491
 2:  0.98 161.22334
 3:  0.96 156.20672
 4:  0.94 155.69111
 5:  0.92 154.73372
 6:  0.90 147.72752
 7:  0.88 147.42015
 8:  0.86 145.28493
 9:  0.84 139.61195
10:  0.82 139.31720
11:  0.80 135.85975
12:  0.78 134.82415
13:  0.76 132.99036
14:  0.74 131.04479
15:  0.72 128.72170
16:  0.70 127.44643
17:  0.68 127.40141
18:  0.66 127.16448
19:  0.64 124.38002
20:  0.62 120.80639
21:  0.60 119.73960
22:  0.58 118.51230
23:  0.56 118.48179
24:  0.54 111.71706
25:  0.52 110.16883
26:  0.50 106.59738
27:  0.48 102.69194
28:  0.46 101.16956
29:  0.44  99.42315
30:  0.42  99.05635
31:  0.40  98.61746
32:  0.38  96.34093
33:  0.36  94.12098
34:  0.34  93.13552
35:  0.32  91.59512
36:  0.30  90.34894
37:  0.28  90.20442
38:  0.26  86.92543
39:  0.24  86.80526
40:  0.22  85.10398
41:  0.20  80.45166
42:  0.18  79.36364
43:  0.16  77.57389
44:  0.14  75.72578
45:  0.12  73.11364
46:  0.10  70.52604
47:  0.08  66.32262
48:  0.06  65.21262
49:  0.04  62.66528
50:  0.02  57.35800
51:  0.00  47.91623
    quant       val

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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