谢谢你看我的问题!
我有用于3项任务的病人绩效的下列(虚拟)数据:
patient_df = data.frame(id = seq(1:5),
age = c(30,72,46,63,58),
education = c(11, 22, 18, 12, 14),
task1 = c(21, 28, 20, 24, 22),
task2 = c(15, 15, 10, 11, 14),
task3 = c(82, 60, 74, 78, 78))> patient_df
id age education task1 task2 task3
1 1 30 11 21 15 82
2 2 72 22 28 15 60
3 3 46 18 20 10 74
4 4 63 12 24 11 78
5 5 58 14 22 14 78我还提供了以下基于年龄和教育的截止值的虚拟查找表,以将患者的绩效定义为在每项任务中受损或未受损:
cutoffs = data.frame(age = rep(seq(from = 35, to = 70, by = 5), 2),
education = c(rep("<16", 8), rep(">=16",8)),
task1_cutoff = c(rep(24, 16)),
task2_cutoff = c(11,11,11,11,10,10,10,10,9,13,13,13,13,12,12,11),
task3_cutoff = c(rep(71,8), 70, rep(74,2), rep(73, 5)))> cutoffs
age education task1_cutoff task2_cutoff task3_cutoff
1 35 <16 24 11 71
2 40 <16 24 11 71
3 45 <16 24 11 71
4 50 <16 24 11 71
5 55 <16 24 10 71
6 60 <16 24 10 71
7 65 <16 24 10 71
8 70 <16 24 10 71
9 35 >=16 24 9 70
10 40 >=16 24 13 74
11 45 >=16 24 13 74
12 50 >=16 24 13 73
13 55 >=16 24 13 73
14 60 >=16 24 12 73
15 65 >=16 24 12 73
16 70 >=16 24 11 73我的目标是在patient_df中创建3个新变量,这些变量用二进制指示符指示病人在每项任务中是否受损。例如,对于id=1 in patient_df,他们的年龄是<=35,他们的教育程度<16岁,因此task1的临界值为24,task2的临界值为11,task3的临界值为71,因此低于这些值的分数将表示损伤。
我希望通过在截止数据集中引用与年龄和教育相关的截止值来对每个id进行此操作,以便结果如下所示:
> goal_patient_df
id age education task1 task2 task3 task1_impaired task2_impaired task3_impaired
1 1 30 11 21 15 82 1 1 0
2 2 72 22 28 15 60 0 0 1
3 3 46 18 20 10 74 1 1 0
4 4 63 12 24 11 78 1 0 0
5 5 58 14 22 14 78 1 0 0实际上,我的patient_df有600+患者,并且有7+任务,每个任务都有与年龄和教育相关的临界值,所以我们非常感谢采用“干净”的方法来完成这一任务!我现在唯一能想到的选择是编写大量的if_else语句或case_whens,对于其他使用我的代码的人来说,这不会是难以置信的可复制性:(
提前谢谢你!
发布于 2020-06-12 21:59:30
我建议将查找表和patient_df数据文件放在长格式中。我认为这可能更容易管理多个任务。
您的education列是数字列;因此转换为字符"<16“或">=16”将有助于查找表中的匹配。
使用fuzzy_inner_join将数据与查找表匹配,其中任务和教育与==完全匹配,但如果为每个查找表行指定一个年龄范围,则age将在age_low和age_high之间进行匹配。
最后,对特定任务的两个数据帧的值进行比较,计算出impaired值。
请注意输出时,缺少1的id,因为它超出了查找表的年龄范围。您可以向该表中添加更多行以解决此问题。
library(tidyverse)
library(fuzzyjoin)
cutoffs_long <- cutoffs %>%
pivot_longer(cols = starts_with("task"), names_to = "task", values_to = "cutoff_value", names_pattern = "task(\\d+)") %>%
mutate(age_low = age,
age_high = age + 4) %>%
select(-age)
patient_df %>%
pivot_longer(cols = starts_with("task"), names_to = "task", values_to = "patient_value", names_pattern = "(\\d+)") %>%
mutate(education = ifelse(education < 16, "<16", ">=16")) %>%
fuzzy_inner_join(cutoffs_long, by = c("age" = "age_low", "age" = "age_high", "education", "task"), match_fun = list(`>=`, `<=`, `==`, `==`)) %>%
mutate(impaired = +(patient_value < cutoff_value))输出
# A tibble: 12 x 11
id age education.x task.x patient_value education.y task.y cutoff_value age_low age_high impaired
<int> <dbl> <chr> <chr> <dbl> <chr> <chr> <dbl> <dbl> <dbl> <int>
1 2 72 >=16 1 28 >=16 1 24 70 74 0
2 2 72 >=16 2 15 >=16 2 11 70 74 0
3 2 72 >=16 3 60 >=16 3 73 70 74 1
4 3 46 >=16 1 20 >=16 1 24 45 49 1
5 3 46 >=16 2 10 >=16 2 13 45 49 1
6 3 46 >=16 3 74 >=16 3 74 45 49 0
7 4 63 <16 1 24 <16 1 24 60 64 0
8 4 63 <16 2 11 <16 2 10 60 64 0
9 4 63 <16 3 78 <16 3 71 60 64 0
10 5 58 <16 1 22 <16 1 24 55 59 1
11 5 58 <16 2 14 <16 2 10 55 59 0
12 5 58 <16 3 78 <16 3 71 55 59 0https://stackoverflow.com/questions/62352017
复制相似问题