1

问题 我想创建一个 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 执行

4

1 回答 1

1

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)

鉴于您的数据的结构,您可以使用以下代码。我假设的地方regionstate并且唯一purposequarter标识您的数据中的一行。

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)whereyearqare 整数创建的类型。

于 2022-01-24T03:51:31.247 回答