从现有数据创建 tsibble 时如何设置索引和键?

How to set index and key when creating tsibble from existing data?

问题 我想创建一个 tsibble,但不清楚如何设置键以及如何设置索引。一点也不乱。

我尝试了以下方法:

ts <- t %>%
  as_tsibble(
    key = t$key_field, 
    index = c(t$date, to$state), 
    regular = FALSE
  )

但出现错误:

Error: Can't subset columns that don't exist.
x Columns `AA`, `AA`, `AA`, `AA`, =etc. don't exist.

There is NO columns named 'AA', this `AA`, `AA`, `AA`, `AA` is actually the data!

需要示例 希望能从另一个现有 data.frame 来源的数据收集中创建真实世界的 tsibble。

需要讨论 讨论使用现有数据创建 tsibble 和定义索引、键。我提供的代码抱怨

** 数据 **

地区 (chr)、州 (chr)、目的 (chr)、旅行 (dbl)、年份 (dbl)

1998 Q1 Adelaide    South Australia Business    135.0776903 1998
1998 Q2 Adelaide    South Australia Business    109.9873160 1998
1998 Q3 Adelaide    South Australia Business    166.0346866 1998
1998 Q4 Adelaide    South Australia Business    127.1604643 1998
1999 Q1 Adelaide    South Australia Business    137.4485333 1999
1999 Q2 Adelaide    South Australia Business    199.9125861 1999
1999 Q3 Adelaide    South Australia Business    169.3550898 1999
1999 Q4 Adelaide    South Australia Business    134.3579372 1999
2000 Q1 Adelaide    South Australia Business    154.0343980 2000
2000 Q2 Adelaide    South Australia Business    168.7763637 2000
2005 Q3 Australia's Coral Coast Western Australia   Business    28.6365371  2005
2005 Q4 Australia's Coral Coast Western Australia   Business    26.4668880  2005
2006 Q1 Australia's Coral Coast Western Australia   Business    19.0804140  2006
2006 Q2 Australia's Coral Coast Western Australia   Business    25.8851570  2006
2006 Q3 Australia's Coral Coast Western Australia   Business    35.5701650  2006
2006 Q4 Australia's Coral Coast Western Australia   Business    16.8853340  2006
2007 Q1 Australia's Coral Coast Western Australia   Business    34.5039748  2007
2007 Q2 Australia's Coral Coast Western Australia   Business    21.6070762  2007
2007 Q3 Australia's Coral Coast Western Australia   Business    38.6497565  2007
2007 Q4 Australia's Coral Coast Western Australia   Business    26.0811098  2007

这是另一个错误的尝试:

创建一个tsibble把数据放在第一个位置。引用具有未引号的列名称的列。 key + index 需要唯一定义一行。索引应该是代表您的时间单位的单个列。

library(ggplot2)
library(tsibble)

# txhousing is data that comes with ggplot2 and has a timeseries
# of housing sales data for each city in the state of Texas.
data(txhousing)

as_tsibble(txhousing, key = city, index = date)
# or 
tsibble(txhousing, key = city, index = date)

鉴于您的数据结构,您可以使用以下代码。 我假设 regionstatepurpose 以及 quarter 唯一标识数据中的一行。

as_tsibble(tourism, key = c(region, state, purpose), index = quarter)

# if there are years where you are missing quarters 
# or if you are missing years in the middle of a time series
# use regular = FALSE
as_tsibble(tourism, key = c(region, state, purpose), index = quarter, regular = FALSE)

此处 quarteryearquarter 类型,您可以使用 tsibble::make_quarter(year, q) 创建它,其中 yearq 是整数。