在我的数据中,我想计算出所执行的医疗保健活动的绝对频率、相对频率和每个病人平均执行的次数。
我使用了以下代码来计算医疗保健利用率:
Df %>%
group_by(A) %>%
summarize(n = n()) %>%
mutate(rel.freq = (n/sum(n))*100) %>%
mutate(avg.A.pt = n/sum(n_distinct(Person[A == A])))
我对代码的最后一行有问题。我需要计算一种特定类型的护理的每个病人的活动数量,计算为活动n
的总数除以唯一的n_distinct(Person)
患者数,但只除以接受该特定类型护理Person[HCU == HCU]
的患者。
我的目标是这样的结果:
*HCU n rel.freq avg.hcu.pt*
ECG 486 10% 4.0
Echo 301 8% 1.8
你能帮我修一下密码吗?
提前谢谢你!
在回复之后,还提供了一些额外的信息:
我在安全的环境中使用远程访问,因此不幸的是,我无法向您提供数据的示例。我有一个大约20.000名病人的数据集,他们接受了11.000.000个医疗活动(行)和34列,例如专业、医疗中心、年龄和个人代码。在我的文章中,我想说明的是:-至少接受过一次特定保健活动(我称之为相对频率)的(独特的)患者的百分比--每个(特定类型的)患者的医疗活动的平均数量。
基本上,我已经绘制了护理的类型,例如使用group_by和dplyr过滤器的实验室测试,这给了我实验室测试的总数。但现在我想说明一下,例如,有多少病人至少做过一次核磁共振,有多少人从未做过核磁共振,有多少人(平均)接受了MRI检查。
我尝试了你的建议
Df %>%
Group_by(A, Person) %>%
Summarise(n = n())
# A= healthcare activities
这给了我:
A Person n
MRI 1 6
MRI 2 2
… for all >1000 patients who received MRI
Echo 1 3
And so on
我怎样才能得到MRI患者的百分比?每个病人平均MRI的数目是多少?
发布于 2019-03-04 21:29:41
让我们创建一些玩具数据。四种不同概率的治疗方法。100例患者就诊1000次。
set.seed(123)
df<-data.frame(A = sample(c("MRI", "ECG", "Echo", "PET"), 1000,
prob=c(0.05, 0.8, 0.13, 0.02), replace=TRUE),
p = sample(1:100, 1000, replace=TRUE))
现在我们聚合数据
df %>%
# group by Treatment and patients
group_by(A, p) %>%
# first summary is the number of a specific treatments for each patient
summarise(n = n()) %>%
# next summary we sum the number distinct patients in the group
# and divide by sum the number of distinct patients to get the rel.freq of the treatment.
# Then we take the mean value of the number of treatment pr. patient
summarise(rel.freq = n_distinct(p)/n_distinct(df$p),
avg.hcu.pt = mean(n))
结果
# A tibble: 4 x 3
A rel.freq avg.hcu.pt
<fct> <dbl> <dbl>
1 ECG 1 8.02
2 Echo 0.76 1.72
3 MRI 0.37 1.30
4 PET 0.17 1.12
https://stackoverflow.com/questions/54985023
复制相似问题