R 具有重复值的扩展时间序列
传播时间序列数据的更好方法是什么。我有重复的观察结果,所以我包括了R 具有重复值的扩展时间序列,r,R,传播时间序列数据的更好方法是什么。我有重复的观察结果,所以我包括了I=row_number(),但这造成了引入NAs的新问题,现在又出现了重复的日期 x <- portfolio_prices %>% mutate(i = row_number()) %>% spread(cusip, prc) 我想预告数据,但只有一个日期,而不是重复的日期 x <- portfolio_prices %>% mutate(i = row_number()) %>
I=row_number()
,但这造成了引入NAs的新问题,现在又出现了重复的日期
x <- portfolio_prices %>%
mutate(i = row_number()) %>%
spread(cusip, prc)
我想预告数据,但只有一个日期,而不是重复的日期
x <- portfolio_prices %>%
mutate(i = row_number()) %>%
spread(cusip, prc)
数据:
portfolio_prices考虑到您希望传播的变量,我在您提供的数据集中没有看到重复的数据
library(dplyr)
library(tidyr)
portfolio_prices %>%
nrow()
portfolio_prices %>%
as_tibble() %>%
distinct(cusip, date_ret) %>%
nrow()
这样你就可以直接传播
portfolio_prices %>%
as_tibble() %>%
spread(cusip, prc)
如果您确实有重复项,那么您可能希望在变异前使用groupby
,如下所示:
portfolio_prices %>%
as_tibble() %>%
group_by(date_ret) %>%
mutate(row = 1:n()) %>%
ungroup() %>%
spread(cusip, prc)
您能提供预期输出的示例吗?我尝试了预期输出。如果可能的话,我只想尝试折叠当前输出。谢谢!我认为你的第一种方法对我很管用。只需使用distinct
删除重复的值即可。第二种和第三种方法产生的结果与我面对的NA值增加的结果相同。i、 e.投资组合价格由4666个观察值组成,使用distinct
将其减少到251个观察值,这是有意义的,因为一年中大约有251个“工作日”。其他两种方法给出了4666个观测值,其中我有一个包含许多NAs的宽数据帧(或者我有19个“2009_01_01”).19表示cusip
的数量。在进行任何分析之前,请确保您的数据集。如果这回答了您的问题,请在接受我的回答时将其标记为已解决,这样就不会浪费时间再次尝试回答。谢谢。
portfolio_prices %>%
as_tibble() %>%
group_by(date_ret) %>%
mutate(row = 1:n()) %>%
ungroup() %>%
spread(cusip, prc)