我正在尝试使用data.frame
排列dplyr
降序的所有变量。我尝试过这个post和other,但没有运气。
我可以使用sapply(my_data, function(x) sort(x, decreasing = T))
来完成它,但我想知道是否有dplyr
方式。
谢谢。
预期输出:
## A tibble: 14 x 8
# mean_EDR total_EDR mean_ED total_ED meanEDGE totalEDGE PD SR
# <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
# 1 0.006364422 0.25457688 87.45347 3591.964 4.402291 190.8988 5012.058 45
# 2 0.004558913 0.18287734 85.17115 3585.592 4.334637 182.0548 5006.400 43
# 3 0.004354222 0.18235650 82.18129 3577.188 4.296272 180.4434 4847.001 42
# 4 0.003916397 0.17623788 81.62851 3428.398 4.257691 177.3847 4826.244 42
# 5 0.003371391 0.13485566 81.39085 3287.251 4.242196 176.0916 4761.613 42
# 6 0.002764087 0.11609165 80.42706 3255.634 4.236135 173.6815 4553.213 41
# 7 0.002600013 0.10400050 79.82142 3217.082 4.207982 173.5540 4479.349 41
# 8 0.002490112 0.09960447 76.88072 3152.110 4.186332 171.0301 4376.620 40
# 9 0.002375897 0.09604333 76.62444 3112.277 4.171466 170.3076 4329.670 40
#10 0.002233566 0.09503590 74.10183 3066.979 4.132238 168.3193 4326.234 40
#11 0.002177910 0.09147222 73.85801 3064.977 4.125226 167.4533 4239.042 40
#12 0.002125356 0.08713958 71.32510 2954.320 4.113421 164.5368 4195.718 40
#13 0.002111953 0.08447812 71.26738 2850.695 4.108972 164.3589 4178.706 40
#14 0.001883719 0.07723249 69.17816 2767.127 4.101265 164.0506 3951.562 40
我的数据:
mydata <- structure(list(mean_EDR = c(0.00217790991358218, 0.0022335657178876,
0.00636442199939338, 0.00237589747478451, 0.00391639742140039,
0.00212535568313589, 0.00337139144056166, 0.00455891259894763,
0.00211195297458007, 0.00435422243053321, 0.00249011168862914,
0.00276408679487771, 0.00260001258489477, 0.00188371927668553
), total_EDR = c(0.0914722163704516, 0.0960433258691667, 0.254576879975735,
0.0950358989913805, 0.176237883963018, 0.0871395830085717, 0.134855657622467,
0.182356503957905, 0.0844781189832028, 0.182877342082395, 0.0996044675451654,
0.116091645384864, 0.104000503395791, 0.0772324903441067), mean_ED = c(74.1018288513791,
71.3251043864098, 71.2673767111652, 69.1781633678164, 79.8214230265286,
87.4534730091657, 80.4270622827382, 82.1812860167843, 73.8580061679473,
85.1711489295962, 76.6244373025454, 81.6285146116456, 81.3908547968942,
76.880719592627), total_ED = c(3112.27681175792, 3066.97948861562,
2850.69506844661, 2767.12653471266, 3591.96403619379, 3585.59239337579,
3217.08249130953, 3287.25144067137, 2954.32024671789, 3577.18825504304,
3064.97749210182, 3428.39761368911, 3255.63419187577, 3152.10950329771
), meanEDGE = c(4.13223792760153, 4.12522568181855, 4.10126475315219,
4.10897215839184, 4.24219626910329, 4.23613487246523, 4.18633177964857,
4.40229086298827, 4.11342097420357, 4.33463744539261, 4.20798247355965,
4.29627178239136, 4.25769090087689, 4.17146614021448), totalEDGE = c(173.553992959264,
177.384704318198, 164.050590126088, 164.358886335674, 190.898832109648,
173.681529771074, 167.453271185943, 176.091634519531, 164.536838968143,
182.05477270649, 168.319298942386, 180.443414860437, 170.307636035076,
171.030111748794), PD = c(4376.619712149, 4326.234320518, 4178.705568494,
3951.562387357, 4847.001017106, 5006.400370579, 4553.213048526,
4479.348501062, 4329.670425728, 5012.05828655, 4239.042466738,
4826.243742531, 4761.612966919, 4195.718301322), SR = c(42, 43,
40, 40, 45, 41, 40, 40, 40, 42, 40, 42, 40, 41)), class = c("tbl_df",
"tbl", "data.frame"), row.names = c(NA, -14L), .Names = c("mean_EDR",
"total_EDR", "mean_ED", "total_ED", "meanEDGE", "totalEDGE",
"PD", "SR"))
答案 0 :(得分:4)
我们sort
列独立的类似选项会循环map
和sort
library(dplyr)
library(purrr)
mydata %>%
map_df(sort, decreasing = TRUE)
# A tibble: 14 x 8
# mean_EDR total_EDR mean_ED total_ED meanEDGE totalEDGE PD SR
# <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
# 1 0.00636 0.255 87.5 3592 4.40 191 5012 45.0
# 2 0.00456 0.183 85.2 3586 4.33 182 5006 43.0
# 3 0.00435 0.182 82.2 3577 4.30 180 4847 42.0
# 4 0.00392 0.176 81.6 3428 4.26 177 4826 42.0
# 5 0.00337 0.135 81.4 3287 4.24 176 4762 42.0
# 6 0.00276 0.116 80.4 3256 4.24 174 4553 41.0
# 7 0.00260 0.104 79.8 3217 4.21 174 4479 41.0
# 8 0.00249 0.0996 76.9 3152 4.19 171 4377 40.0
# 9 0.00238 0.0960 76.6 3112 4.17 170 4330 40.0
#10 0.00223 0.0950 74.1 3067 4.13 168 4326 40.0
#11 0.00218 0.0915 73.9 3065 4.13 167 4239 40.0
#12 0.00213 0.0871 71.3 2954 4.11 165 4196 40.0
#13 0.00211 0.0845 71.3 2851 4.11 164 4179 40.0
#14 0.00188 0.0772 69.2 2767 4.10 164 3952 40.0
或另一种选择是在arrange
map
mydata %>%
map(~ .x %>%
as_tibble %>%
arrange(desc(value))) %>%
bind_cols %>%
set_names(names(mydata))