【发布时间】:2020-11-21 18:43:19
【问题描述】:
假设这个数据框:
country <- c('USA', 'USA', 'USA', 'USA', 'USA', 'UK', 'UK', 'UK', 'Canada')
number <- c(1:9)
df <- data.frame(country, number)
我希望能够仅对国家计数大于 4 或小于 2 的行进行子集化。所以在这种情况下,它将返回:
country number
USA 1
USA 2
USA 3
USA 4
USA 5
Canada 9
我可以让它工作:
totalcounts <- filter(count(df, country), n>4 | n<2) # giving me a df of the country and count
for (i in nrow(totalcounts)){
# code in here that rbinds rows as it matches
}
但我觉得必须有一个更简单的方法。我还没有掌握 sapply 之类的东西,所以我觉得我在这里遗漏了一些东西。看起来我正在走很长的路,并且已经有一些东西可以做到这一点。
【问题讨论】: