【问题标题】:How to remove individuals with less than n occurences in each month?如何删除每个月出现次数少于 n 的个人?
【发布时间】:2021-05-22 11:31:38
【问题描述】:

我有一个包含物种出现次数(TD_threshold)的data.frame。这种情况分为不同的个体和不同的月份。我的目标是选择每个月出现超过 40 次的个人。应用以下代码时,某些月份没有出现但其他月份超过 40 个的个体仍然存在。

set.seed(28)

TD_samples <-
  TD_threshold %>%
  as.data.frame() %>%
  group_by(ind_id, month) %>%
  sample_n(40)

head(TD_threshold)
# A tibble: 6 x 5
# Groups:   ind_id, month [1]
  time                ind_id month      lng      lat
  <dttm>              <fct>  <dbl>    <dbl>    <dbl>
1 2015-01-01 09:40:23 BAV3       1 -588886. 1373720.
2 2015-01-01 10:40:06 BAV3       1 -618226. 1372848.
3 2015-01-01 12:00:06 BAV3       1 -655789. 1353752.
4 2015-01-01 13:30:06 BAV3       1 -701669. 1349417.
5 2015-01-01 15:00:06 BAV3       1 -727293. 1335201.
6 2015-01-02 10:40:06 BAV3       1 -726753. 1334190.

【问题讨论】:

    标签: r filter group-by point


    【解决方案1】:

    你可以试试这个解决方案 -

    library(dplyr)
    
    TD_threshold %>%
      mutate(year_month = format(time, '%Y-%m')) %>%
      add_count(ind_id, year_month) %>%
      group_by(ind_id) %>%
      filter(all(n > 40)) %>%
      ungroup
    

    【讨论】:

      猜你喜欢
      • 2020-12-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-07
      • 2011-07-29
      相关资源
      最近更新 更多