我有一个 Dataframe ,其中有一行不良事件,还有这些不良事件与手术的关系,如下所示:
df <- data.frame(
adverse_event = c(
"Haemorrhage", "related", "likely related",
"Other", "related", "likely related", "Pain", "related", "likely related",
"Subcapsular hematoma", "related", "likely related", "Ascites",
"related", "likely related", "Hyperbilirubinemia", "related",
"likely related", "Liver abscess", "related", "likely related",
"Pleural effusion with drainage", "related", "likely related",
"Pneumothorax", "related", "likely related", "Biliary leakage / occlusion / fistula",
"related", "likely related", "Portal vein thrombosis", "related",
"likely related", "Sepsis", "related", "likely related"
),
grade_1 = c(
4L, 4L, 0L, 3L, 6L, 1L, 8L, 4L, 5L, 3L, 1L, 3L, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA
),
grade_2 = c(
2L, 3L, 0L, 11L, 3L, 7L, 2L, 4L, 2L, 1L, 2L, 0L, 1L, 1L, 0L,
1L, 0L, 2L, 1L, 1L, 0L, 1L, 2L, 1L, 1L, 1L, 0L, NA, NA, NA, NA,
NA, NA, NA, NA, NA
),
grade_3 = c(
1L, 4L, 1L, 5L, 3L, 2L, 2L, 5L, 1L, NA, NA, NA, NA, NA, NA,
NA, NA, NA, 4L, 5L, 1L, NA, NA, NA, 1L, 1L, 0L, 1L, 2L, 0L, 1L,
1L, 0L, 1L, 1L, 0L
),
grade_4 = c(
2L, 4L, 1L, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA
)
)
现在,我想按字母顺序对不良事件进行排序,但当然要将“相关”、“可能相关”行与单个不良事件行放在一起,所以我想先对它们进行分组。在本例中,通常为3行,但假设有时为2行,也有4或5行(字符串/名称中包含“相关”的不良事件行除外,但例如“不太可能相关”)。
我知道,我可以通过grep('related', df$adverse_event, invert = T)
获得不良事件行的索引,但我不确定如何在排序之前使用它将行分组在一起。
编辑:所需输出的左列开始:
expected_output_left_column <- data.frame(adverse_event = c(
"Ascites", "related", "likely related",
"Biliary leakage / occlusion / fistula", "related", "likely related" ) )
谢谢大家!
6条答案
按热度按时间jaxagkaj1#
使用dplyr中的基r和
lead
函数的另一种解决方案of1yzvn42#
您可以执行以下操作:
输出:
请注意,这里使用的是
data.table::nafill()
。完整的data.table
解决方案如下所示:mwngjboj3#
添加“group”变量并排序
fhity93d4#
再加上另一个
tidyverse
解决方案:解释
mutate + fill
:用词干标记每个adverse_event
,即用上述相应事件重新标记所有related
记录。1.嵌套所有列,但保留新创建的
grp
列,该列具有股骨柄不良事件的名称。1.对不良事件股骨柄进行分类。
1.再次取消嵌套行。
1.删除
grp
列。btxsgosb5#
使用
rank
的方法。使用具有4个“腹水”条目的扩展数据集。扩展数据
eqoofvh96#
以下是不同建议的基准(如果需要):