【问题标题】:Find the first value that meet a defined criteria查找满足定义条件的第一个值
【发布时间】:2020-03-25 22:43:35
【问题描述】:

我有一个按日期和国家/地区划分的 Coivd-19 病例和死亡数据集。我希望找到每个国家发生第一次死亡的日期,并过滤掉所有之前的日子。你将如何在 R/Tidyverse 中解决这个问题?

library(readxl)
library(httr)
url <- paste("https://www.ecdc.europa.eu/sites/default/files/documents/COVID-19-geographic-disbtribution-worldwide-",format(Sys.time(), "%Y-%m-%d"), ".xlsx", sep = "")
GET(url, authenticate(":", ":", type="ntlm"), write_disk(tf <- tempfile(fileext = ".xlsx")))
df <- read_excel(tf)

【问题讨论】:

    标签: r tidyverse


    【解决方案1】:

    试试这个:

    library(dplyr) 
    # or library(tidyverse)
    
    df %>%
      arrange(`Countries and territories`, DateRep) %>%
      group_by(`Countries and territories`) %>%
      mutate(Cumulative_Death = cumsum(Deaths)) %>%
      ungroup() %>%
      filter(Cumulative_Death > 0) %>%
      group_by(`Countries and territories`) %>%
      mutate(First_Death_Date = min(DateRep)) 
    

    它添加了新列 Cumulative_Death:死亡总和,直到 DateRepFirst_Death_Date:每个国家/地区首次死亡发生的日期

    【讨论】:

      【解决方案2】:

      由于数据是按日期降序排列的,因此您可以找到最后死亡的行索引,并保留等于或小于该值的行号。

      df %>%
        group_by(`Countries and territories`) %>%
        filter(any(Deaths > 0)) %>%
        filter(row_number() <= max(which(Deaths > 0)))
      
      # A tibble: 1,111 x 9
      # Groups:   Countries and territories [95]
         DateRep               Day Month  Year Cases Deaths `Countries and territories` GeoId Pop_Data.2018
         <dttm>              <dbl> <dbl> <dbl> <dbl>  <dbl> <chr>                       <chr>         <dbl>
       1 2020-03-25 00:00:00    25     3  2020     2      0 Afghanistan                 AF         37172386
       2 2020-03-24 00:00:00    24     3  2020     6      1 Afghanistan                 AF         37172386
       3 2020-03-25 00:00:00    25     3  2020    23      1 Albania                     AL          2866376
       4 2020-03-24 00:00:00    24     3  2020    11      2 Albania                     AL          2866376
       5 2020-03-23 00:00:00    23     3  2020    13      0 Albania                     AL          2866376
       6 2020-03-22 00:00:00    22     3  2020     6      0 Albania                     AL          2866376
       7 2020-03-21 00:00:00    21     3  2020     0      0 Albania                     AL          2866376
       8 2020-03-20 00:00:00    20     3  2020    11      0 Albania                     AL          2866376
       9 2020-03-19 00:00:00    19     3  2020     4      1 Albania                     AL          2866376
      10 2020-03-18 00:00:00    18     3  2020     4      0 Albania                     AL          2866376
      # ... with 1,101 more rows
      

      【讨论】:

      • 谢谢 :) 非常感谢你 :)
      【解决方案3】:
      library(dplyr)
      
      df <- df %>% 
            arrange(DateRep) %>% 
            distinct(`Countries and territories`, .keep_all = TRUE)
      

      我认为这对您来说是最简单的选择。 dplyr::distinct() 删除除第一行以外的所有内容,因此如果您按所需列排列数据,您将只获得第一个匹配项。 .keep_all = TRUE 将保留所有列,而不仅仅是 distinct() 中指定的列。

      【讨论】:

      • 感谢您的回答,但我不太明白这个逻辑。并且它会返回我想要的东西:)
      猜你喜欢
      • 2020-03-08
      • 2021-05-15
      • 1970-01-01
      • 1970-01-01
      • 2018-05-16
      • 1970-01-01
      • 2019-11-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多