假设数据框是这样的:
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"
谁能告诉我如何解决这个问题?谢谢!
答案 0 :(得分:5)
如果您想n()
观察次数少于2的群组,我们可以按'x'分组,然后df %>%
group_by(x) %>%
filter(n()<2)
# x y
#1 b 003
#2 c 005
分组行数(var tip = d3.tip()
.attr('class', 'd3-tip')
.offset([-10, 0])
.html(function(d) {
return "<span style='color:red'>" + d3.format("$,")(d.values.reduce(function(sum, d){
return sum + d.amount;
})) + "</span>";
})
)小于2。 / p>
ObjectRefinery