如何使用 count(x)>2 这样的过滤器删除观察值?

How to remove observations with filters like count(x)>2?

假设数据框是这样的:

df <- data.frame(x = c("a", "a", "b", "a", "c"), y = c("001", "002", "003", "004", "005"))

我只想保留记录:

	x	y
	b	003
	c	005

为了得到这个结果,我这样做了:

df %>% filter (count(x)<2) -> df1

我收到了这个错误:

Error: no applicable method for 'group_by_' applied to an object of class "factor"

谁能告诉我如何解决这个问题?谢谢!

如果你想 filter 少于 2 个观察值的组,我们可以按 'x' 分组,然后 filter 行数 (n()) less比 2.

df %>%
   group_by(x) %>% 
   filter(n()<2)
#  x   y
#1 b 003
#2 c 005