谢谢你看了我的问题!我有以下3个任务的患者绩效数据(假人)。
我有以下(虚拟)数据,用于记录患者在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个新的变量,用二进制指标来表示病人在每个任务上是否受损。例如,对于病人_df中的id=1,他们的年龄是<=35岁,受教育程度是<16岁,那么任务1的分界值是24,任务2的分界值是11,任务3的分界值是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,这对于使用我的代码的其他人来说,是不可置信的可重复性:(
先谢谢你
我建议将您的查找表和 patient_df
数据框架的长格式。我想这可能更容易管理多个任务。
你的 education
列是数字,所以转换为字符"<16 "或">=16 "将有助于在查找表中进行匹配。
使用"<16 "或">=16 "将有助于在查找表中进行匹配。fuzzy_inner_join
会将数据与查找表进行匹配,其中任务和教育完全匹配。==
不过 age
会间 age_low
和 age_high
如果你为每个查找表的行指定了年龄范围。
最后: impaired
是通过比较特定任务的两个数据帧的值来计算的。
请注意输出。id
1缺少,因为不在查找表中的年龄范围内。你可以在该表中添加更多的行来解决这个问题。
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 0