'Error in xts: 'order.by' cannot contain 'NA', 'NaN', or 'Inf'

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?



Solution 1:[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.

Solution 2:[2]

In C++17, you might use if constexpr to avoid suntime branching (which might be optimized anyway):

template <int size>
struct Foo
{
    // ...

    int GetValue()
    {
        if constexpr (size == 1)
        {
            return -3;
        }
        return 4;
    }
};

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Joshua Ulrich
Solution 2