【问题标题】:How to describe unique values of grouped observations for several vars?如何描述几个变量的分组观察的唯一值?
【发布时间】:2021-09-05 09:55:45
【问题描述】:

我有一个可以多次观察每个患者的小标题。所以名字是这样的: id_患者(人数); id_eval (num) ; treat_1(逻辑); treat_2(逻辑); treat_1_type (char) ; treat_2_type (char)。

我想要的:一个描述独特值的汇总表(带有 tbl_summary),以了解有多少患者至少有 1 次关注某种可能性。像这样:

var All patients (n=N)
treat_1 AA (aa %)
treat_2 BB (bb %)
treat_1_type
- Type_1 CC (cc %)
- Type_2 DD (dd %)
treat_2_type
- Type_1 EE (ee %)
- Type_2 FF (ff %)
- Type_3 GG (gg %)

我现在拥有的是:

evals %>%
    group_by(id_patient) %>%
    select(id_patient, treat_1, treat_2) %>%
    summarise(across(everything(), .fns = unique))
    summary()

但这给了我所有现有的 TRUE/FALSE 组合,因此它并不代表真正独特的值。这是逻辑部分,所以很简单,它不适用于因素......

你觉得我怎么能做到这一点?

【问题讨论】:

标签: r tidyverse unique


【解决方案1】:

我希望你能给我们一些数据。 但让我们自己生产。

library(tidyverse)

n=10
evals = tibble(
  id_patient = sample(1:50, n, replace = T),
  id_eval = sample(120:277, n),
  treat_1 = sample(c(T, F), n, replace = T),
  treat_2 = sample(c(T, F), n, replace = T),
  treat_1_type = sample(c("Type_1", "Type_2"), n, replace = T),
  treat_2_type = sample(c("Type_1", "Type_2", "Type_3"), n, replace = T)
)

evals

输出

# A tibble: 10 x 6
   id_patient id_eval treat_1 treat_2 treat_1_type treat_2_type
        <int>   <int> <lgl>   <lgl>   <fct>        <fct>       
 1         42     237 TRUE    FALSE   Type_2       Type_3      
 2         24     240 FALSE   FALSE   Type_1       Type_1      
 3         10     236 TRUE    FALSE   Type_1       Type_3      
 4         27     153 TRUE    FALSE   Type_1       Type_2      
 5         29     126 TRUE    FALSE   Type_2       Type_1      
 6         18     194 FALSE   TRUE    Type_1       Type_2      
 7         18     215 TRUE    FALSE   Type_2       Type_2      
 8         48     205 TRUE    FALSE   Type_1       Type_3      
 9         12     131 FALSE   FALSE   Type_1       Type_2      
10         13     225 FALSE   FALSE   Type_2       Type_3         

没事吧?但愿如此。 现在让我们随心所欲地做一个总结。

seval = evals %>%
  group_by(id_patient) %>%
  summarise(
    treat_1 = sum(treat_1)>0,
    treat_2 = sum(treat_2)>0,
    treat_1_Type_1 = sum(treat_1_type=="Type_1")>0,
    treat_1_Type_2 = sum(treat_1_type=="Type_2")>0,
    treat_2_Type_1 = sum(treat_2_type=="Type_1")>0,
    treat_2_Type_2 = sum(treat_2_type=="Type_2")>0,
    treat_2_Type_3 = sum(treat_2_type=="Type_3")>0
  ) %>% summarise(
    treat_1 = sum(treat_1),
    treat_2 = sum(treat_2),
    treat_1_Type_1 = sum(treat_1_Type_1),
    treat_1_Type_2 = sum(treat_1_Type_2),
    treat_2_Type_1 = sum(treat_2_Type_1),
    treat_2_Type_2 = sum(treat_2_Type_2),
    treat_2_Type_3 = sum(treat_2_Type_3)
  )


输出

# A tibble: 1 x 7
  treat_1 treat_2 treat_1_Type_1 treat_1_Type_2 treat_2_Type_1 treat_2_Type_2 treat_2_Type_3
    <int>   <int>          <int>          <int>          <int>          <int>          <int>
1       6       1              6              4              2              4              4

现在您可以轻松计算比例

seval %>% 
  pivot_longer(everything(), names_to = "var", values_to = "val") %>% 
  group_by(var) %>% 
  mutate(prop = val/length(unique(evals$id_patient)))

输出

# A tibble: 7 x 3
# Groups:   var [7]
  var              val  prop
  <chr>          <int> <dbl>
1 treat_1            6 0.667
2 treat_2            1 0.111
3 treat_1_Type_1     6 0.667
4 treat_1_Type_2     4 0.444
5 treat_2_Type_1     2 0.222
6 treat_2_Type_2     4 0.444
7 treat_2_Type_3     4 0.444  

我测试了 chrfactor 变量的所有内容,一切正常。

【讨论】:

    猜你喜欢
    • 2017-06-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多