【问题标题】:How can I select rows from dataframe based on date and value?如何根据日期和值从数据框中选择行?
【发布时间】:2020-09-02 17:40:59
【问题描述】:

我有一个数据框,其中包含许多国家/地区及其不同日期的总病例数和新病例数。它看起来如下:

  iso_code continent     location date       total_cases new_cases stringency_index population
  <chr>    <chr>         <chr>    <chr>            <dbl>     <dbl>            <dbl>      <dbl>
1 ABW      North America Aruba    2020-03-13           2         2              0       106766
2 ABW      North America Aruba    2020-03-19          NA        NA             33.3     106766
3 ABW      North America Aruba    2020-03-20           4         2             33.3     106766
4 ABW      North America Aruba    2020-03-21          NA        NA             44.4     106766
5 ABW      North America Aruba    2020-03-22          NA        NA             44.4     106766
6 ABW      North America Aruba    2020-03-23          NA        NA             44.4     106766

我能够过滤数据框以获取 new_cases >= 5 的所有行:

df_filtered <- df %>% filter(new_cases >= 5)

但是,这给了我 new_cases 等于或大于 5 的所有行:

  iso_code continent     location date       total_cases new_cases stringency_index population
  <chr>    <chr>         <chr>    <chr>            <dbl>     <dbl>            <dbl>      <dbl>
1 ABW      North America Aruba    2020-03-24          12         8             44.4     106766
2 ABW      North America Aruba    2020-03-25          17         5             44.4     106766
3 ABW      North America Aruba    2020-03-27          28         9             44.4     106766
4 ABW      North America Aruba    2020-03-30          50        22             85.2     106766
5 ABW      North America Aruba    2020-04-01          55         5             85.2     106766
6 ABW      North America Aruba    2020-04-03          60         5             85.2     106766

我怎样才能只得到这个条件成立的最早/第一个日期的行?

这是我的输出理想的样子:

  iso_code continent     location           date       total_cases new_cases stringency_index population
  <chr>    <chr>         <chr>              <chr>            <dbl>     <dbl>            <dbl>      <dbl>
1 ABW      North America Aruba              2020-03-24          12         8             44.4     106766
2 AFG      Asia          Afghanistan        2020-03-16          16         6             38.9     38928341
3 AGO      Africa        Angola             2020-04-19          24         5             90.7     32866268
4 ALB      Europe        Albania            2020-03-13          23        12             78.7     2877800
5 AND      Europe        Andorra            2020-03-17          14         9             31.4     77265
6 ARE      Asia          Utd. Arab Emirates 2020-02-28          19         6              8.3     9890400

【问题讨论】:

  • 查看 slice_max 和 slice_min 函数
  • 你可以试试df_filtered &lt;- df %&gt;% filter(new_cases &gt;= 5 &amp; date==min(date))
  • @Duck - 缺少group_by,并且该复合条件仅在该国在其第一个日期至少有 5 个病例时才会成立,而不是条件成立的第一个日期。
  • @Duck 我试过了,但它只返回整个数据集的最低日期
  • @asd7 由于没有提供有意义的数据,请尝试df %&gt;% group_by(continent) %&gt;% filter(new_cases &gt;= 5) %&gt;% filter(date==min(date))df %&gt;% group_by(continent) %&gt;% filter(new_cases &gt;= 5) %&gt;% filter(date==first(date))

标签: r dataframe


【解决方案1】:

试试这个:

df %>% 
  group_by(iso_code) %>%  ## within each country (group)
  filter(new_cases >= 5) %>%  ## keep rows where there are at least 5 cases
  slice_min(date, n = 1, with_ties = FALSE)  ## then keep the row with the smallest date

【讨论】:

    【解决方案2】:

    我让它与以下代码一起工作:

    df_filtered <- df %>% filter(new_cases >= 5) #filter all new_cases with at least 5
    
    df_sorted <- df_filtered %>%                 #group by country and arrange by date,
      group_by(iso_code) %>%                     #then get the first row of every 
      arrange(date) %>%                          #group 
      slice(1L)
    

    灵感来自这个问题的答案Earliest Date for each id in R

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-07-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-14
      • 2021-03-28
      • 2020-02-10
      相关资源
      最近更新 更多