【问题标题】:Calculation averages per patient using n_distinct with condition使用带有条件的 n_distinct 计算每位患者的平均值
【发布时间】:2019-07-25 20:53:45
【问题描述】:

在我的数据框中,我想计算执行医疗保健活动的绝对频率、相对频率和每位患者执行的平均次数。

我使用以下代码计算医疗保健利用率:

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,有多少人从未接受过 MRI,患者接受了多少次 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 患者的百分比?以及每位患者的平均核磁共振检查次数?

【问题讨论】:

  • 一个可重现的数据框将帮助我们找到问题。一个问题是“Person”在按“A”分组然后生成一个汇总值“n”后不可用。也许您想按 Person 和 A 分组?然后总结 n=n() 给出医疗保健活动 pr 的数量。人。您可以再次汇总以获取所有人的总活动。
  • 感谢您的回复!我在问题中添加了一些文字

标签: r dataframe dplyr average distinct-values


【解决方案1】:

让我们创建一些玩具数据。四种不同概率的处理。 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

【讨论】:

  • 谢谢,这正是我需要的!
猜你喜欢
  • 2017-09-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-09-13
  • 2018-04-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多