【问题标题】:R: subsetting a dataframe according to a condition by idR:根据 id 条件对数据帧进行子集化
【发布时间】:2021-05-25 13:07:08
【问题描述】:

我有以下数据集:

Lines <- "id Observation_code Observation_value
1  A       5
1  A       6
1  B       24
2  C       2
2  D       9
2  A       12
3  V       5
3  E       6
3  C       24
4  B       2
4  D       9
4  C       12"

dat <- read.table(text = Lines, header = TRUE)

我想以某种方式对数据进行子集化,以获取 Observation_code == A 患者的全部历史记录。在这个例子中,因为只有 id 1 和 2 有 observation_code A,所以它们应该是剩下的。请注意,id 1 和 2 的所有观察值都应该在最终数据集中:

Final <- "id Observation_code Observation_value
1  A       5
1  A       6
1  B       24
2  C       2
2  D       9
2  A       12"

dat_Final <- read.table(text = Final, header = TRUE)

【问题讨论】:

    标签: r subset


    【解决方案1】:

    基础 R

    ind <- ave(dat$Observation_code == "A", dat$id, FUN = any)
    dat[ind,]
    #   id Observation_code Observation_value
    # 1  1                A                 5
    # 2  1                A                 6
    # 3  1                B                24
    # 4  2                C                 2
    # 5  2                D                 9
    # 6  2                A                12
    

    do.call(rbind, by(dat, dat$id, FUN = function(z) z[any(z$Observation_code == "A"),]))
    

    dplyr

    library(dplyr)
    dat %>%
      group_by(id) %>%
      filter(any(Observation_code == "A")) %>%
      ungroup()
    # # A tibble: 6 x 3
    #      id Observation_code Observation_value
    #   <int> <chr>                        <int>
    # 1     1 A                                5
    # 2     1 A                                6
    # 3     1 B                               24
    # 4     2 C                                2
    # 5     2 D                                9
    # 6     2 A                               12
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-16
      • 2022-06-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多