【问题标题】:Creating new variable based on more than one condition基于多个条件创建新变量
【发布时间】:2021-06-15 15:47:41
【问题描述】:

我正在尝试根据某些条件创建一个新变量。我有以下数据:

df <- data.frame(ID = c("A1","A1","A2","A2","A3","A4","A4"),
                 type = c("small","large","small","large","large","small","large"),
                 code = c("B9", "[0,20]","B9","[20,40]","[0,20]","B9","[40,60]" ))

这给出了:

    ID  type   code
1   A1  small   B9
2   A1  large   [0,20]
3   A2  small   B9
4   A2  large   [20,40]
5   A3  large   [0,20]
6   A4  small   B9
7   A4  large   [40,60]

我想创建一个新变量(code2),它基于 type == largecode 的相应值,同时按 ID。所以 ID - A1 应该有 [0,20] 作为它的代码 2。我想实现以下目标:

    ID  type   code       code2
1   A1  small   B9        [0,20]    
2   A1  large   [0,20]    [0,20] 
3   A2  small   B9        [20,40]
4   A2  large   [20,40]   [20,40]
5   A3  large   [0,20]    [0,20] 
6   A4  small   B9        [40,60]
7   A4  large   [40,60]   [40,60]

据我所知,我正在尝试使用dplyrifelse,但没有运气。

【问题讨论】:

    标签: r


    【解决方案1】:

    我们可以在dplyr 中使用分组操作,即按“ID”分组,提取“类型”值为“大”的“代码”(假设每个“类型”中没有重复值)身份证'

    library(dplyr)
    df <- df %>% 
       group_by(ID) %>%
       mutate(code2 = code[type == 'large']) %>%
       ungroup
    

    -输出

    df
    # A tibble: 7 x 4
      ID    type  code    code2  
      <chr> <chr> <chr>   <chr>  
    1 A1    small B9      [0,20] 
    2 A1    large [0,20]  [0,20] 
    3 A2    small B9      [20,40]
    4 A2    large [20,40] [20,40]
    5 A3    large [0,20]  [0,20] 
    6 A4    small B9      [40,60]
    7 A4    large [40,60] [40,60]
    

    如果有重复,使用match,它会给出第一个匹配索引的索引

    df <- df %>%
           group_by(ID) %>%
           mutate(code2 = code[match('large', type)]) %>%
           ungroup
    

    【讨论】:

    • 谢谢。如果每个 ID 中的“类型”值重复,我该如何使用?
    • 谢谢。我尝试了match 并得到 [0,20] 对所有行重复。它应该是这样工作的吗?
    • @cajt 你用过group_by(ID)
    • 我用了group_by(ID),但没有用。
    • @cajt 我在您的示例中尝试了该方法。它提供与第一个解决方案相同的输出
    【解决方案2】:

    data.table 选项

    > setDT(df)[, code2 := code[type == "large"], ID][]
       ID  type    code   code2
    1: A1 small      B9  [0,20]
    2: A1 large  [0,20]  [0,20]
    3: A2 small      B9 [20,40]
    4: A2 large [20,40] [20,40]
    5: A3 large  [0,20]  [0,20]
    6: A4 small      B9 [40,60]
    7: A4 large [40,60] [40,60]
    

    【讨论】:

      猜你喜欢
      • 2021-09-28
      • 1970-01-01
      • 2019-04-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多