【问题标题】:pulling and using current data in the dataset提取和使用数据集中的当前数据
【发布时间】:2020-05-06 12:20:13
【问题描述】:
 df <- read.csv ('https://raw.githubusercontent.com/ulklc/covid19- 
  timeseries/master/countryReport/raw/rawReport.csv',
            stringsAsFactors = FALSE)

  yesterday <- function() Sys.Date() - 1L
  yesterday()
 # [1] "if it doesn't work yesterday()-1  do it"

我创建了昨天的函数。

df 数据集中有区域、死亡和恢复部分。

他想要你帮助的事情就是这个。

在 DF 数据集中查找与昨天相比,该大陆报告死亡人数最多的国家

根据昨天在 DF 数据集中,找到该大陆上报告恢复最多的国家。

所以我想要的输出。

国家名称地区死亡恢复日期

2020/05/05 意大利(样本)欧洲 600(样本)50

2020/05/05 西班牙(样本)欧洲 200(样本)580

2020/05/05 中国(样本)亚洲1200 80

2020/05/05 日本(样本)亚洲 400 780

..

..

..

国家和数据作为例子。

一条死亡线。 恢复了一行。

每个地区都需要。

有 5 个区域。它变成了 10 行。

【问题讨论】:

    标签: r


    【解决方案1】:

    根据您的要求,此输出为 10 行(每个地区 2 行),但请注意,例如美国报告的美洲地区死亡人数和康复人数最多。这意味着它出现了两次。伊朗和澳大利亚也是如此,因此只有 7 个不同的行。

    library(tidyverse)
    death_df <- df %>%
      filter(as.Date(day) == yesterday()) %>%
      group_by(region) %>%
      filter(death == max(death)) %>%
      select(Date = day,
             countryName,
             region,
             death,
             recovered)
    
    recovered_df <- df %>%
      filter(as.Date(day) == yesterday()) %>%
      group_by(region) %>%
      filter(recovered == max(recovered)) %>%
      select(Date = day,
             countryName,
             region,
             death,
             recovered)
    
    full_df <- bind_rows(death_df, recovered_df)
    full_df
    # A tibble: 10 x 5
    # Groups:   region [5]
       Date       countryName    region   death recovered
       <chr>      <chr>          <chr>    <int>     <int>
     1 2020/05/05 Australia      Oceania     96      5889
     2 2020/05/05 Algeria        Africa     470      2067
     3 2020/05/05 United Kingdom Europe   29427         0
     4 2020/05/05 Iran           Asia      6340     80475
     5 2020/05/05 United States  Americas 72241    199684
     6 2020/05/05 Australia      Oceania     96      5889
     7 2020/05/05 Spain          Europe   25613    154718
     8 2020/05/05 Iran           Asia      6340     80475
     9 2020/05/05 United States  Americas 72241    199684
    10 2020/05/05 South Africa   Africa     148      2746
    

    【讨论】:

      猜你喜欢
      • 2020-12-17
      • 2021-04-07
      • 2011-04-18
      • 1970-01-01
      • 2013-04-26
      • 2020-06-21
      • 1970-01-01
      • 1970-01-01
      • 2014-07-30
      相关资源
      最近更新 更多