删除少于9个唯一观察值的ID

时间:2019-05-20 01:12:45

标签: r dplyr unique

我正尝试过滤我的数据并删除ID少于9的独立月份观察值。我还想创建一个包含计数的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的过滤条件不等于逻辑向量

3 个答案:

答案 0 :(得分:2)

您可以使用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

答案 1 :(得分:1)

我们可以使用n_distinct

要删除少于9个唯一观察值的ID

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

答案 2 :(得分:0)

这是一种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")
相关问题