我有一个数据框,我将其转换为tsibble
时间序列对象,以便更轻松地对数据进行时间序列图形和操作(滚动时间窗口分析)。我每天都会获得新数据,我想将其附加到表示为的原始数据框中df
,新的传入数据表示为df2
。我可以将这些data.frame
'tsibble
独立地更改为对象,但是当我使用rbind()
先加入它们然后使用as_tsibble
时,会出现错误。
as_tsibble(final_df, index = date, key = ticker)
Error: A valid tsibble must have distinct rows identified by key and index.
i Please use duplicates() to check the duplicated rows.
在这里设置问题是reprex的代码。
df <- data.frame(ticker = c("UST10Y", "UST2Y", "AAPL", "SPX", "BNO"),
buy_price = c(62.00, 68.00, 37.00, 55.00, 41.00),
sale_price = c(64.00, 71.00, 42.00, 60.00, 45.00),
close_price = c(63.00, 70.00, 38.00, 56.00, 43.00),
date = c(as.Date("April 29th, 2021", "April 29th, 2021", "April 29th, 2021", "April 29th, 2021", "April 29th, 2021")))
df2 <- data.frame(ticker = c("UST10Y", "UST2Y", "AAPL", "SPX", "BNO"),
buy_price = c(63.00, 69.00, 38.00, 53.00, 44.00),
sale_price = c(66.00, 77.00, 47.00, 63.00, 48.00),
close_price = c(65.00, 74.00, 39.00, 55.00, 45.00),
date = c(as.Date("April 30th, 2021", "April 30th, 2021", "April 30th, 2021", "April 30th, 2021", "April 30th, 2021")))
final_df <- rbind(df,df2)
str(final_df)
> 'data.frame': 10 obs. of 5 variables:
as_tsibble(final_df, index = date, key = ticker)
运行代码as_tsibble(final_df, index = date, key = ticker)
后,顺序也更改为按字母顺序排列,而我想保留原始顺序(另一个问题)。
我无法用 来创建 tsibble final_df
,尽管可以在和tsibble
上单独创建a 。df
df2
我是否遗漏了什么,或者不可能有一个tsibble
具有多行相同股票名称的对象?