我已经被这个问题困扰了几个小时,这变得有点令人沮丧。基本上我想排列一些数据,以便 NA 基于分组结构首先出现。我可以做到其中的一部分,但我所做的一切都无法达到预期的结果。
有了这个代码,
df <- df |>
group_by(AESOC, AEPT) |>
arrange(!is.na(AEPT), !is.na(Severity), .by_group = TRUE)
我已经能够实现图中所示的效果。
但我仍然想进一步安排,以便第 9-12 行出现在第 1 行之前,第 25-28 行出现在第 13 行之前(即在 AESOC 和 AEPT 确定的组的最开始处。
这个小数据包含在这里:
df <- structure(list(AESOC = c("Blood and lymphatic system disorders",
"Blood and lymphatic system disorders", "Blood and lymphatic system disorders",
"Blood and lymphatic system disorders", "Blood and lymphatic system disorders",
"Blood and lymphatic system disorders", "Blood and lymphatic system disorders",
"Blood and lymphatic system disorders", "Blood and lymphatic system disorders",
"Blood and lymphatic system disorders", "Blood and lymphatic system disorders",
"Blood and lymphatic system disorders", "Cardiac disorders",
"Cardiac disorders", "Cardiac disorders", "Cardiac disorders",
"Cardiac disorders", "Cardiac disorders", "Cardiac disorders",
"Cardiac disorders", "Cardiac disorders", "Cardiac disorders",
"Cardiac disorders", "Cardiac disorders", "Cardiac disorders",
"Cardiac disorders", "Cardiac disorders", "Cardiac disorders"
), AEPT = c(" Anaemia", " Anaemia", " Anaemia", " Anaemia",
" Lymphopenia", " Lymphopenia", " Lymphopenia", " Lymphopenia",
NA, NA, NA, NA, " Dizziness", " Dizziness", " Dizziness",
" Dizziness", " Palpitations", " Palpitations", " Palpitations",
" Palpitations", " Presyncope", " Presyncope", " Presyncope",
" Presyncope", NA, NA, NA, NA), Severity = c(" mild",
" moderate", " severe", NA, " mild", " moderate",
" severe", NA, " mild", " moderate", " severe",
NA, " mild", " moderate", " severe", NA,
" mild", " moderate", " severe", NA, " moderate",
" mild", " severe", NA, " moderate", " mild",
" severe", NA)), row.names = c(NA, -28L), class = c("tbl_df",
"tbl", "data.frame"))
任何帮助将不胜感激。
您可以通过以下方式使用
arrange
:
library(dplyr)
df %>% arrange(AESOC, !is.na(AEPT), !is.na(Severity))
返回:
AESOC AEPT Severity
1 Blood and lymphatic system disorders <NA> <NA>
2 Blood and lymphatic system disorders <NA> mild
3 Blood and lymphatic system disorders <NA> moderate
4 Blood and lymphatic system disorders <NA> severe
5 Blood and lymphatic system disorders Anaemia <NA>
6 Blood and lymphatic system disorders Lymphopenia <NA>
7 Blood and lymphatic system disorders Anaemia mild
8 Blood and lymphatic system disorders Anaemia moderate
9 Blood and lymphatic system disorders Anaemia severe
10 Blood and lymphatic system disorders Lymphopenia mild
11 Blood and lymphatic system disorders Lymphopenia moderate
12 Blood and lymphatic system disorders Lymphopenia severe
13 Cardiac disorders <NA> <NA>
14 Cardiac disorders <NA> moderate
15 Cardiac disorders <NA> mild
16 Cardiac disorders <NA> severe
17 Cardiac disorders Dizziness <NA>
18 Cardiac disorders Palpitations <NA>
19 Cardiac disorders Presyncope <NA>
20 Cardiac disorders Dizziness mild
21 Cardiac disorders Dizziness moderate
22 Cardiac disorders Dizziness severe
23 Cardiac disorders Palpitations mild
24 Cardiac disorders Palpitations moderate
25 Cardiac disorders Palpitations severe
26 Cardiac disorders Presyncope moderate
27 Cardiac disorders Presyncope mild
28 Cardiac disorders Presyncope severe