【问题标题】:Convert tibble to time series将 tibble 转换为时间序列
【发布时间】:2021-08-11 21:26:08
【问题描述】:

我尝试下载经济学人的 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.

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

【问题讨论】:

  • 您尝试了哪些代码以及它究竟是如何失败的?
  • 我已编辑问题以提供更多输入

标签: r tibble tsibble


【解决方案1】:

有效的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>

【讨论】:

  • 感谢您提供此解决方案。我仍然无法使用该数据框。我不能轻易地提取意大利的所有数据,然后在这些地区采取手段,以便在意大利的每个 start_date 都有一个数据点。如果数据是标准 R 格式(矩阵、数组、列表),我可以轻松做到这一点。
【解决方案2】:

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

1) 每月 这会从指定的test 列中创建一个每月动物园对象z,该列按国家/地区拆分并聚合以生成每月时间序列。然后它会从中创建一个 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) 每天如果您想要时间为日期的系列,则省略 FUN 参数并直接使用 zoo。

z <- read.zoo(test[c("end_date", "country", "covid_deaths")], 
  split = "country", aggregate = sum)  

【讨论】:

  • 已扩展至 3 种方法。
猜你喜欢
  • 1970-01-01
  • 2020-08-02
  • 1970-01-01
  • 2016-11-22
  • 1970-01-01
  • 2021-12-10
  • 2020-12-16
  • 2013-06-07
  • 2021-08-02
相关资源
最近更新 更多