假设数据框是这样的:
df <- data.frame(x = c("a", "a", "b", "a", "c"), y = c("001", "002", "003", "004", "005"))
Run Code Online (Sandbox Code Playgroud)
我只想保留记录:
x y b 003 c 005
为了得到这个结果,我这样做了:
df %>% filter (count(x)<2) -> df1
Run Code Online (Sandbox Code Playgroud)
我收到了这个错误:
Error: no applicable method for 'group_by_' applied to an object of class "factor"
Run Code Online (Sandbox Code Playgroud)
谁能告诉我如何解决这个问题?谢谢!
如果你想要filter观察到少于2个的组,我们可以按"x"分组,然后filter将行数(n())小于2.
df %>%
group_by(x) %>%
filter(n()<2)
# x y
#1 b 003
#2 c 005
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
51 次 |
| 最近记录: |