【问题标题】:Remove IDs with fewer than 9 unique observations删除具有少于 9 个唯一观察值的 ID
【发布时间】:2019-05-20 01:12:45
【问题描述】:

我正在尝试过滤我的数据并删除具有少于 9 个唯一月份观察值的 ID。我还想创建一个包含计数的 ID 列表。

我尝试过使用几种不同的选项:

library(dplyr)
count <- bind %>% group_by(IDS) %>% filter(n(data.month)>= 9) %>%       ungroup()
count2 <- subset(bind, with(bind, IDS %in% names(which(table(data.month)>=9))))

这些都不起作用。

这是我的数据的样子:

   data.month   ID
           01    2
           02    2
           03    2
           04    2
           05    2
           05    2
           06    2
           06    2
           07    2
           07    2
           07    2
           07    2
           07    2
           08    2
           09    2
           10    2
           11    2
           12    2
           01    5
           01    5
           02    5
           01    7
           01    7
           01    7
           01    4
           02    4
           03    4
           04    4
           05    4
           05    4
           06    4
           06    4
           07    4
           07    4
           07    4
           07    4
           07    4
           08    4
           09    4
           10    4
           11    4
           12    4

最后,我想要一个:

IDs
2
3

我也想要这个

IDs  Count
2     12
5     2
7     1
4     12

到目前为止,这段代码是最接近的,但仍然只是给出错误代码:

count <- bind %>%
  group_by(IDs) %>% 
  filter(length(unique(bind$data.month >=9)))

filter_impl(.data, quo) 中的错误: 参数 2 过滤条件不计算为逻辑向量

【问题讨论】:

    标签: r dplyr unique


    【解决方案1】:

    您可以使用uniquelength

    library(dplyr)
    df %>% group_by(ID) %>% summarise(Count=length(unique(data.month)))
    # A tibble: 4 x 2
         ID Count
      <int> <int>
    1     2    12
    2     4    12
    3     5     2
    4     7     1
    

    如果要获取ID

    df%>%group_by(ID)%>%summarise(Count=length(unique(data.month)))%>%filter(Count>9)%>%select(ID)
    # A tibble: 2 x 1
         ID
      <int>
    1     2
    2     4
    

    【讨论】:

      【解决方案2】:

      我们可以使用n_distinct

      删除具有少于 9 个唯一观察值的 IDs

      library(dplyr)
      
      df %>%
        group_by(ID) %>%
        filter(n_distinct(data.month) >= 9) %>%
        pull(ID) %>% unique
      
      #[1] 2 4
      

      或者

      df %>%
        group_by(ID) %>%
        filter(n_distinct(data.month) >= 9) %>%
        distinct(ID)
      
      #     ID
      #  <int>
      #1     2
      #2     4
      

      对于每个 ID 的唯一计数

      df %>%
        group_by(ID) %>%
        summarise(count = n_distinct(data.month))
      
      
      #     ID count
      #   <int> <int>
      #1     2    12
      #2     4    12
      #3     5     2
      #4     7     1
      

      【讨论】:

        【解决方案3】:

        这是data.table 方法

        library( data.table )
        

        具有 9 个或更多观察的 ID

        unique( DT[, if (.N >= 9) .SD, by = .(data.month)]$ID )
        #[1] 2 4
        

        #每个月的唯一 ID

        unique(DT, by = c("data.month", "ID"))[, .(counts = .N), by = .(IDs = ID)]
        #    IDs counts
        # 1:   2     12
        # 2:   5      2
        # 3:   7      1
        # 4:   4     12
        

        样本数据

        DT <- fread("data.month   ID
                   01    2
                    02    2
                    03    2
                    04    2
                    05    2
                    05    2
                    06    2
                    06    2
                    07    2
                    07    2
                    07    2
                    07    2
                    07    2
                    08    2
                    09    2
                    10    2
                    11    2
                    12    2
                    01    5
                    01    5
                    02    5
                    01    7
                    01    7
                    01    7
                    01    4
                    02    4
                    03    4
                    04    4
                    05    4
                    05    4
                    06    4
                    06    4
                    07    4
                    07    4
                    07    4
                    07    4
                    07    4
                    08    4
                    09    4
                    10    4
                    11    4
                    12    4")
        

        【讨论】:

          猜你喜欢
          • 2016-10-16
          • 1970-01-01
          • 2022-11-23
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多