gpt4 book ai didi

r - 如何使用 count(x)>2 等过滤器删除观察值?

转载 作者:行者123 更新时间:2023-12-04 09:43:23 27 4
gpt4 key购买 nike

假设数据框是这样的:

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

我只想保留记录:
	x	y	b	003	c	005

To get this result, I did this:

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() ) 小于 2。

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

关于r - 如何使用 count(x)>2 等过滤器删除观察值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31818132/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com