Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 201839

Why do i lose my date index in this r script, calculating returns?

$
0
0

We have used dplyr for this specific problem and found a helpful script online for the task at hand the only problem is that this script keeps removing the time index which we need later on. The script goes as followed:

  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()  

This is the result of 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")))

We are getting the return for all the stocks in prices %>%. We have tried to cut out the remove_rownames() %>% and such but then the data frame would not load at all.

Would appreciate any help, thank you.


Viewing all articles
Browse latest Browse all 201839

Trending Articles