【问题标题】:In R: remove with multiple conditions across variables在 R 中:跨变量删除多个条件
【发布时间】:2021-04-02 11:49:41
【问题描述】:

我想删除那些组为 1 的观察(按 companyID),并且一年有两个观察(其中一个观察涉及 B 类,另一个涉及 K 类)。通过我下面的示例可能更容易理解。

这是我拥有的数据集的示例:

companyID  type  group  year
    1       B      1    2006
    1       K      1    2006
    1       B      1    2007
    2       B      1    2001
    2       B      1    2002
    2       K      1    2002
    2       K      2    2003
    3       B      1    2010
    3       K      1    2010
    3       K      2    2011

这是我最喜欢的结果:

companyID  type  group  year
    1       B      1    2007
    2       B      1    2001
    2       K      2    2003
    3       K      2    2011

谢谢!!

【问题讨论】:

    标签: r


    【解决方案1】:

    将此代码/语法用于tidyverse

    library(dplyr)
    
    df %>% group_by(companyID, year) %>%
      filter(n() == 1 , n_distinct(type) == 1)
    
    # A tibble: 4 x 4
    # Groups:   companyID, year [4]
      companyID type  group  year
          <int> <chr> <int> <int>
    1         1 B         1  2007
    2         2 B         1  2001
    3         2 K         2  2003
    4         3 K         2  2011
    

    第二个条件不起作用,因此在您的给定示例中是多余的。但是,正如您所指定的,我已将其作为条件包含在内。以下也将起作用

    df %>% group_by(companyID, year) %>%
      filter(!n() > 1 , !n_distinct(type) > 1)
    

    baseR方式

    subset(df, as.numeric(ave(df$type, paste(df$companyID, df$year),  FUN = length)) == 1)
    
       companyID type group year
    3          1    B     1 2007
    4          2    B     1 2001
    7          2    K     2 2003
    10         3    K     2 2011
    

    subset(df, as.numeric(ave(df$companyID, paste(df$companyID, df$year),  FUN = length)) == 1 & 
             as.numeric(ave(df$type, paste(df$companyID, df$year),  FUN = function(x) length(unique(x)))) == 1)
    

    【讨论】:

      【解决方案2】:

      基础溶液

      do.call(
        rbind,
        by(df,list(df$companyID,df$group,df$year),function(x){
          if ((nrow(x)!=2 & x$group[1]==1) | x$group[1]!=1) {
            x
          }
        })
      )
      
         companyID type group year
      4          2    B     1 2001
      7          2    K     2 2003
      3          1    B     1 2007
      10         3    K     2 2011
      

      【讨论】:

        【解决方案3】:

        data.table 解决方案:

        library(data.table)
        
        dat = fread('companyID  type  group  year
            1       B      1    2006
            1       K      1    2006
            1       B      1    2007
            2       B      1    2001
            2       B      1    2002
            2       K      1    2002
            2       K      2    2003
            3       B      1    2010
            3       K      1    2010
            3       K      2    2011')
        
        result = dat[, types:= uniqueN(type), by = .(companyID, group, year)][group != 1 | types == 1][, types:= NULL]
        
        > result
           companyID type group year
        1:         1    B     1 2007
        2:         2    B     1 2001
        3:         2    K     2 2003
        4:         3    K     2 2011
        

        【讨论】:

          【解决方案4】:

          在我看来,您只需按groupyear 分组即可。

          df %>% group_by(group, year) %>% filter(n() < 2)
          

          输出:

          #   companyID type  group  year
          #       <dbl> <chr> <dbl> <dbl>
          # 1         1 B         1  2007
          # 2         2 B         1  2001
          # 3         2 K         2  2003
          # 4         3 K         2  2011
          

          【讨论】:

            猜你喜欢
            • 2020-10-01
            • 1970-01-01
            • 2020-05-12
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2021-06-23
            相关资源
            最近更新 更多