我已经在这个问题上纠结了好几个小时,这有点令人沮丧。基本上,我想安排一些数据,以便 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"))
任何帮助都将不胜感激。