【问题标题】:How to choose groups where there are more than 5 observations如何选择观察值超过 5 个的组
【发布时间】:2017-06-27 21:22:24
【问题描述】:

我有包含 3 列的数据集。

price
Factor (numeric var 1-15 categories)
group (string (100 categories))

我需要为每个因素类别只选择这些观察超过五个的组!怎么做?

这是不正确的!

new<-city[(city$group)>5,]

【问题讨论】:

  • 使用?table 计算每个因子的频率,然后计算子集

标签: r


【解决方案1】:

我们可以通过data.table 做到这一点。将'data.frame'转换为'data.table',按'Factor'、'group'分组,得到行数大于5的Data.table(.SD)的子集(.N &gt;5

library(data.table)
setDT(city)[, .SD[.N >5], by = .(Factor, group)]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-09-24
    • 2020-01-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-11
    • 1970-01-01
    • 2012-07-10
    相关资源
    最近更新 更多