为什么我会在这个r脚本中丢失日期索引,计算返回值?

问题描述 投票:0回答:1

我们已经针对该特定问题使用了dplyr,并在线找到了一个有用的脚本来处理当前的任务,唯一的问题是该脚本不断删除以后需要的时间索引。脚本如下:

  Asset_Returns_dplyr_byhand <- 
  prices %>% 
  to.monthly(indexAt = "lastof", OHLC = FALSE) %>%
  data.frame(date = index(.)) %>%
  gather(asset, returns, -date) %>% 
  group_by(asset) %>%  
  mutate(returns = (log(returns) - log(lag(returns)))) %>%
  spread(asset, returns) %>% 
  remove_rownames() %>% 
  select(date, symbols) %>% 
  na.omit()  

这是dput(head(prices))的结果:

structure(c(NA, NA, NA, NA, NA, NA, 230.039993, 230.25, 229.449997, 
223.990005, 225.160004, 223.899994, 11.622857, 11.747143, 10.852858, 
10.448571, 10.492857, 10.398571, 301.086456, 302.496185, 304.369141, 
297.370392, 302.640625, 305.250854, 2726.919922, 2734.77002, 
2704.590088, 2637.919922, 2639.280029, 2629.870117), class = c("xts", 
"zoo"), .indexCLASS = "Date", .indexTZ = "UTC", tclass = "Date", tzone = "UTC", src = "yahoo", updated = structure(1573297901.44073, class = c("POSIXct", 
"POSIXt")), index = structure(c(1335830400, 1335916800, 1336003200, 
1336089600, 1336348800, 1336435200), tzone = "UTC", tclass = "Date"), .Dim = 6:5, .Dimnames = list(
    NULL, c("FB", "AMZN", "NFLX", "GOOG", "NDX")))

我们正在获得prices %>%中所有股票的回报。我们试图切出remove_rownames() %>%等,但随后数据帧将根本无法加载。

非常感谢您的帮助,谢谢。

r date indexing return
1个回答
0
投票

现在问题中的dput(head(prices))应该可用了。

© www.soinside.com 2019 - 2024. All rights reserved.