0

我试图下载经济学人的 Github 存储库提供的有关 covid 的数据。

library(readr)
library(knitr)
myfile <- "https://raw.githubusercontent.com/TheEconomist/covid-19-excess-deaths-tracker/master/output-data/excess-deaths/all_weekly_excess_deaths.csv"
test <- read_csv(myfile)

我得到的是一个 tibble 数据框,我无法轻松访问存储在该 tibble 中的数据。我想看一列,说test$covid_deaths_per_100k并将其重新塑造成矩阵或ts对象,其中行表示时间,列表示国家。

我手动尝试过,但失败了。然后我尝试使用该tsibble软件包并再次失败:

tsibble(test[c("covid_deaths_per_100k","country")],index=test$start_date)
Error: Must extract column with a single valid subscript.
x Subscript `var` has the wrong type `date`.
ℹ It must be numeric or character.

所以,我想问题是数据是按国家堆积的,因此时间索引是重复的。我需要一些神奇的管道功能来完成这项工作吗?有没有一种简单的方法可以做到这一点,也许不需要管道?

4

2 回答 2

0

ts 最适用于月度、季度或年度系列。在这里,我们展示了一些方法。

1) 月度z这会从指定的列中创建一个月度动物园对象,这些test列按国家/地区拆分并聚合以生成月度时间序列。然后它从中创建一个 ts 对象。

library(zoo)

z <- read.zoo(test[c("start_date", "country", "covid_deaths")], 
  split = "country", FUN = as.yearmon, aggregate = sum)
as.ts(z)

2)每周创建一个频率为53的每周ts对象

to_weekly <- function(x) {       
      yr <- as.integer(as.yearmon(x))
      wk <- as.integer(format(as.Date(x), "%U"))
      yr + wk/53
}
z <- read.zoo(test[c("start_date", "country", "covid_deaths")], 
  split = "country", FUN = to_weekly, aggregate = sum)  
as.ts(z)

3) daily如果你想要一个以时间为日期的系列,那么省略 FUN 参数并直接使用 zoo。

z <- read.zoo(test[c("end_date", "country", "covid_deaths")], 
  split = "country", aggregate = sum)  
于 2021-05-23T11:42:51.210 回答
0

有效的tsibble必须具有由键和索引标识的不同行:

as_tsibble(test,index = start_date,key=c(country,region))
# A tsibble: 11,715 x 17 [1D]
# Key:       country, region [176]
   country   region    region_code start_date end_date    days  year  week population total_deaths
   <chr>     <chr>     <chr>       <date>     <date>     <dbl> <dbl> <dbl>      <dbl>        <dbl>
 1 Australia Australia 0           2020-01-01 2020-01-07     7  2020     1   25734100         2497
 2 Australia Australia 0           2020-01-08 2020-01-14     7  2020     2   25734100         2510
 3 Australia Australia 0           2020-01-15 2020-01-21     7  2020     3   25734100         2501
 4 Australia Australia 0           2020-01-22 2020-01-28     7  2020     4   25734100         2597
 5 Australia Australia 0           2020-01-29 2020-02-04     7  2020     5   25734100         2510
 6 Australia Australia 0           2020-02-05 2020-02-11     7  2020     6   25734100         2530
 7 Australia Australia 0           2020-02-12 2020-02-18     7  2020     7   25734100         2613
 8 Australia Australia 0           2020-02-19 2020-02-25     7  2020     8   25734100         2608
 9 Australia Australia 0           2020-02-26 2020-03-03     7  2020     9   25734100         2678
10 Australia Australia 0           2020-03-04 2020-03-10     7  2020    10   25734100         2602
# ... with 11,705 more rows, and 7 more variables: covid_deaths <dbl>, expected_deaths <dbl>,
#   excess_deaths <dbl>, non_covid_deaths <dbl>, covid_deaths_per_100k <dbl>,
#   excess_deaths_per_100k <dbl>, excess_deaths_pct_change <dbl>
于 2021-05-23T09:06:07.840 回答