简体   繁体   English

R-寻找累积值

[英]R- Finding cumulative values

I have COVID data.我有 COVID 数据。 It gives the number of cases and deaths of European countries on certain dates.它给出了欧洲国家在某些日期的病例数和死亡人数。 I need to select 10 countries and show the cases after the 1000th confirmed case by drawing a line.我需要 select 10 个国家,并通过画线显示第 1000 例确诊病例之后的病例。 Countries are listed under a column titled countries, number of cases under a column titled case, number of deaths under a column titled deaths, and dates under a column titled date.国家列在标题为国家的列下,病例数列在标题为病例的列下,死亡人数列在标题为死亡的列下,日期列在标题为日期的列下。 The problem is that I don't know how to get the 1000th confirmed case numbers of countries and the number of cases thereafter.问题是我不知道如何获得第1000个国家确诊病例数以及之后的病例数。 Here is the question: "c) The cumulative number of cases of 10 countries by days since 1000th confirmed case"这里是问题:“c)自第1000例确诊病例以来10个国家的累计病例数”

Here is my trying code:这是我的尝试代码:

library(utils)
COVID_data <-read.csv("https://opendata.ecdc.europa.eu/covid19/nationalcasedeath_eueea_daily_ei/csv", na.strings = "", fileEncoding = "UTF-8-BOM")


  above100th_cases <- ten_countries_cases %>% 
  filter(Cumulative_cases_Austria >= 1000|
           Cumulative_cases_Croatia >= 1000|
           Cumulative_cases_Denmark >= 1000|
           Cumulative_cases_Finland >= 1000|
           Cumulative_cases_France >= 1000|
           Cumulative_cases_Germany >= 1000|
           Cumulative_cases_Greece >= 1000|
           Cumulative_cases_Iceland >= 1000|
           Cumulative_cases_Italy >= 1000|
           Cumulative_cases_Spain >= 1000)
  
#Reshape the data set in order to draw line graphs 
above100th_case.m <- melt(above100th_cases,id.vars = 1)

# 0 values were below 100th, then skip these values to reach better graphs
above100th.m <- above100th_case.m %>% filter(value>0)
  
#Draw the graph by using ggplot
ggplot(above100th.m, aes(x=dateRep, y= value, color=variable))+
  geom_line(size=1)+
  #Arrange y axis, by considering min&max cumulative number of cases
  scale_y_continuous(breaks = c(seq(from =5000, to =9000000, by=1001000)))+
  labs(x="Date",
       y="Cumulative Case Number",
       title="The Cumulative Number of Cases By Date") +
  facet_wrap(~variable)+
  theme_bw()

It is obviously wrong.这显然是错误的。 I also tried to use filter(if() else) or when_case(), but I could not apply them.我也尝试使用 filter(if() else) 或 when_case(),但我无法应用它们。 I hope, I could explain myself.我希望,我可以解释自己。

Is this required?这是必需的吗?

library(utils)
COVID_data <-read.csv("https://opendata.ecdc.europa.eu/covid19/nationalcasedeath_eueea_daily_ei/csv", na.strings = "", fileEncoding = "UTF-8-BOM")


library(tidyverse)
COVID_data %>% mutate(dateRep = as.Date(dateRep, '%d/%m/%Y')) %>%
  group_by(countriesAndTerritories) %>%
  arrange(dateRep) %>%
  mutate(cum_cases = cumsum(cases)) %>%
  filter(cum_cases >= 1000) %>%
  ungroup() %>%
  ggplot() +
  geom_line(aes(x = dateRep, y = cum_cases, color = countriesAndTerritories))


Or if 10 countries are to be filtered或者如果要过滤 10 个国家/地区

ten_countries <- c('Denmark', 'Croatia', 'Austria', 'Finland', 'France', 'Germany', 'Greece', 'Iceland', 'Italy', 'Spain')

COVID_data %>% filter(countriesAndTerritories %in% ten_countries) %>%
  mutate(dateRep = as.Date(dateRep, '%d/%m/%Y')) %>%
  group_by(countriesAndTerritories) %>%
  arrange(dateRep) %>%
  mutate(cum_cases = cumsum(cases)) %>%
  filter(cum_cases >= 1000) %>%
  ungroup() %>%
  ggplot() +
  geom_line(aes(x = dateRep, y = cum_cases, color = countriesAndTerritories))

Created on 2021-06-08 by the reprex package (v2.0.0)reprex package (v2.0.0) 于 2021 年 6 月 8 日创建

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM