0

I download time-series data from MSCI: MSCI-daily. I found that the dates are not properly formatted, so I need to deal with it. I use this code to dealing with data-time:

daily$Date <- daily$Date %>% as.Date(format = "%b %d, %Y")
for(i in 2:7){
  daily[,i] <- gsub(daily[,i], pattern = ",", replacement = "") %>% as.numeric()
}
daily <- xts(daily[,-1], order.by = daily[,1])

But it just appear this line:

Error in xts(daily[, -1], order.by = daily[, 1]) : 
  'order.by' cannot contain 'NA', 'NaN', or 'Inf'

Can anyone help me fix this error in r-studio?

Phil
  • 7,287
  • 3
  • 36
  • 66

1 Answers1

1

This works for me.

daily <- read.csv("~/Downloads/MSCI-daily.csv") 
daily[-1] <- lapply(daily[-1], function(x) as.numeric(gsub(",", "", x))) 
daily$Date <- as.Date(daily$Date, format = "%b %d, %Y") 
x <- xts(daily[,-1], daily$Date)

You can use which(is.na(daily$Date)) to find the NA values in the daily data.frame.

Joshua Ulrich
  • 173,410
  • 32
  • 338
  • 418