【问题标题】:Removing individuals from cohort study data based on baseline characteristics根据基线特征从队列研究数据中删除个体
【发布时间】:2019-06-27 16:23:35
【问题描述】:

我有来自一项重复测量的队列研究的健康数据,在该研究中,人们每年都会进行多次随访。在基线(访问 0)中,一些人已经被诊断出患有感兴趣的疾病,而另一些人则没有。当我在分析中查看事件案例时,我需要从我的数据中删除那些在访问 0 时被诊断为“生病”的个人。我怎么能在 tidyverse 中做到这一点?我将在下面包含一个我将要查看的数据结构的示例:

subject_id <- c(1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4,5,5,5,5)
visit <- c(0,1,2,3,0,1,2,3,0,1,2,3,0,1,2,3,0,1,2,3)
diagnosis <- c("not sick", "not sick", "not sick", "sick", "sick", "sick", "sick", "sick", "not sick", "not sick", "sick", "sick", "sick", "sick", "sick", "sick", "not sick", "not sick", "not sick", "sick")

cohort <- data.frame(subject_id, visit, diagnosis)
cohort

【问题讨论】:

  • 感谢您的建议。但是,这在这里不起作用,因为这只会删除访问 0 的行,而不是访问 0 时“生病”的受试者的所有行。

标签: r tidyverse


【解决方案1】:

编辑:如果您想完全删除它们,那么:

cohort %>% 
  group_by(subject_id) %>% 
  mutate(Condn = ifelse(visit==0 & diagnosis=="sick",1,0) ) %>% 
  filter(all(Condn==0))

原创

我们可以做到:

cohort %>% 
  group_by(subject_id) %>% 
   mutate(Condn = ifelse(visit==0 & diagnosis=="sick",1,0) ) %>% 
   filter(Condn==0) %>% 
   ungroup()  %>% 
   select(-Condn)

【讨论】:

  • 运行此代码时,我收到错误消息:"Error in filter(., Condn == 0) : object 'Condn' not found"
  • 使用dplyr::filter。不知道为什么会这样。你能告诉我你使用的完整代码吗?
  • subject_id &lt;- c(1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4,5,5,5,5) visit &lt;- c(0,1,2,3,0,1,2,3,0,1,2,3,0,1,2,3,0,1,2,3) diagnosis &lt;- c("not sick", "not sick", "not sick", "sick", "sick", "sick", "sick", "sick", "not sick", "not sick", "sick", "sick", "sick", "sick", "sick", "sick", "not sick", "not sick", "not sick", "sick") cohort &lt;- data.frame(subject_id, visit, diagnosis) cohort %&gt;% group_by(subject_id) %&gt;% mutate(Condn = ifelse(visit==0 &amp; diagnosis=="sick",1,0) ) %&gt;% filter(Condn==0) %&gt;% ungroup() %&gt;% select(-Condn)
  • 使用dplyr::filter 告诉我。
  • 是的,代码现在可以与您所做的编辑一起使用。感谢您的帮助。
【解决方案2】:

使用dplyr,您可以:

cohort %>%
 group_by(subject_id) %>%
 filter(first(diagnosis) != "sick")

   subject_id visit diagnosis
        <dbl> <dbl> <fct>    
 1          1     0 not sick 
 2          1     1 not sick 
 3          1     2 not sick 
 4          1     3 sick     
 5          3     0 not sick 
 6          3     1 not sick 
 7          3     2 sick     
 8          3     3 sick     
 9          5     0 not sick 
10          5     1 not sick 
11          5     2 not sick 
12          5     3 sick   

或者:

cohort %>%
 group_by(subject_id) %>%
 filter(diagnosis[row_number() == 1] != "sick")

【讨论】:

  • 第一个选项似乎在我运行时将 df 转换为时间序列:Time Series: Start = 1 End = 20 Frequency = 1 [,1] [,2] [,3] 1 1 0 1 2 1 1 1 3 1 2 1 4 1 3 2 5 2 0 2 6 2 1 2 7 2 2 2 8 2 3 2 9 3 0 1 10 3 1 1 11 3 2 2 12 3 3 2 13 4 0 2 14 4 1 2 15 4 2 2 16 4 3 2 17 5 0 1 18 5 1 1 19 5 2 1 20 5 3 2 &gt;
  • 当我运行第二个选项时,我收到此错误:Error: row_number() should only be called in a data context Call `rlang::last_error()` to see a backtrace
  • 我认为您使用了错误的filter。使用dplyr::filter。 @West_End_Line
  • 是的,这就是问题所在!看起来filter() 被基本 R 过滤器掩盖了。我用dplyr::filter 运行它,它成功了。
【解决方案3】:

感谢大家的建议。 @tmfmnk 和 @NelsonGon 都提供了适用于此任务的选项。

我最近从 SAS 转到了 R,这非常有帮助。

【讨论】:

    猜你喜欢
    • 2023-01-24
    • 2014-04-27
    • 2018-12-11
    • 2017-04-01
    • 2014-08-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多