【问题标题】:Selecting observations for which two years are available by country选择按国家/地区提供两年的观测值
【发布时间】:2020-04-11 22:24:01
【问题描述】:

我有一个数据集如下:

DT <- fread(
"ID country year Event_A Event_B
4   BEL   2002  0   1
5   BEL   2002  0   1
6   NLD   2002  1   1
7   NLD   2006  1   0
8   NLD   2006  1   1
9   GBR   2001  0   1
10  GBR   2001  0   0
11  GBR   2001  0   1
12  GBR   2007  1   1
13  GBR   2007  1   1",
header = TRUE)

我想只保留我在两个国家/地区观察到的观察结果。所以,BEL 将退出,因为它只有 2002 年的观察结果。

我想做类似DT[,if(unique(year)&gt;1) .SD, by=country] 的事情,但这并没有做任何事情。我也试过DT[unique(year)&gt;1, .SD, by=country],但这给出了错误:

Error in `[.data.table`(DT, unique(year) > 1, .SD, by = country) : 
  i evaluates to a logical vector length 4 but there are 10 rows. Recycling of logical i is no longer allowed as it hides more bugs than is worth the rare convenience. Explicitly use rep(...,length=.N) if you really need to recycle.

期望的输出:

DT <- fread(
"ID country year Event_A Event_B
6   NLD   2002  1   1
7   NLD   2006  1   0
8   NLD   2006  1   1
9   GBR   2001  0   1
10  GBR   2001  0   0
11  GBR   2001  0   1
12  GBR   2007  1   1
13  GBR   2007  1   1",
header = TRUE)

【问题讨论】:

    标签: r data.table subset


    【解决方案1】:

    如果没有必要在data.table中进行,您可以通过base R按国家/地区计算不同年份的数量:

    country_count <- aggregate(year ~ country, DT, FUN = function(x) NROW(unique(x)))
    DT[DT$country %in% country_count$country[country_count$year > 1],]
    # output
       ID country year Event_A Event_B
    3   6     NLD 2002       1       1
    4   7     NLD 2006       1       0
    5   8     NLD 2006       1       1
    6   9     GBR 2001       0       1
    7  10     GBR 2001       0       0
    8  11     GBR 2001       0       1
    9  12     GBR 2007       1       1
    10 13     GBR 2007       1       1
    

    【讨论】:

      【解决方案2】:

      本着与@user2474226 相同的精神,如果您对其他包开放,一个简单的dplyrsolution:

       library(data.table)
       library(dplyr)
      #> 
      #> Attaching package: 'dplyr'
      #> The following objects are masked from 'package:data.table':
      #> 
      #>     between, first, last
      #> The following objects are masked from 'package:stats':
      #> 
      #>     filter, lag
      #> The following objects are masked from 'package:base':
      #> 
      #>     intersect, setdiff, setequal, union
      
        DT <- fread(
          "ID country year Event_A Event_B
      4   BEL   2002  0   1
      5   BEL   2002  0   1
      6   NLD   2002  1   1
      7   NLD   2006  1   0
      8   NLD   2006  1   1
      9   GBR   2001  0   1
      10  GBR   2001  0   0
      11  GBR   2001  0   1
      12  GBR   2007  1   1
      13  GBR   2007  1   1",
      header = TRUE)
      
        # Detect count of countries
        sel_cnt <-
          DT %>%
          count(country, year) %>%
          count(country) %>%
          filter(n > 1)
      
      
        DT %>%
          semi_join(sel_cnt, by = "country")
      #>   ID country year Event_A Event_B
      #> 1  6     NLD 2002       1       1
      #> 2  7     NLD 2006       1       0
      #> 3  8     NLD 2006       1       1
      #> 4  9     GBR 2001       0       1
      #> 5 10     GBR 2001       0       0
      #> 6 11     GBR 2001       0       1
      #> 7 12     GBR 2007       1       1
      #> 8 13     GBR 2007       1       1
      

      【讨论】:

        【解决方案3】:

        这是使用 ave() 和 subset() 的基本 R 解决方案

        DTout <- subset(DT, as.logical(ave(DT$year,DT$country, FUN = function(x) length(unique(x))>=2)))
        

        这样

        > DTout
           ID country year Event_A Event_B
        3   6     NLD 2002       1       1
        4   7     NLD 2006       1       0
        5   8     NLD 2006       1       1
        6   9     GBR 2001       0       1
        7  10     GBR 2001       0       0
        8  11     GBR 2001       0       1
        9  12     GBR 2007       1       1
        10 13     GBR 2007       1       1
        

        【讨论】:

          【解决方案4】:

          您可以使用uniqueN 获取唯一值的计数并使用.SD 选择行。

          library(data.table)
          DT[, .SD[uniqueN(year) > 1], country]
          
          #   country ID year Event_A Event_B
          #1:     NLD  6 2002       1       1
          #2:     NLD  7 2006       1       0
          #3:     NLD  8 2006       1       1
          #4:     GBR  9 2001       0       1
          #5:     GBR 10 2001       0       0
          #6:     GBR 11 2001       0       1
          #7:     GBR 12 2007       1       1
          #8:     GBR 13 2007       1       1
          

          或者在dplyr 中,我们可以对n_distinct 和filter 做同样的事情

          library(dplyr)
          DT %>% group_by(country) %>% filter(n_distinct(year) > 1)
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2017-04-27
            • 2010-11-01
            相关资源
            最近更新 更多