Q:

How to convert a data.frame table to an xts object

i downloaded price data from Thomson Reuters Datastream into excel and created a csv file. The resulting csv file is a data.frame object. I have already set the date column as a date using the as.date function. The resulting table is a data.frame with 42 columns, the first column being the date column and the other 41 being price data for different companies. The class of the price data is 'numeric'.
The table looks something like this:
       date           TickerA      TickerB      TickerC
    1  2000-01-03       20           NA            40
    2  2000-01-04       21           33            42
    3  2000-01-05       22           32            NA
    4  .
    5  .
    6  .

Note that some prices are missing since some companies have gone bankrupt or were founded later in the observation period. Now, i would like to use the Return.calculate function from the PerformanceAnalytics package, which converts a table of prices to a table of returns, something like this:
      date           TickerA     TickerB     TickerC
   1  2000-01-03       NA          NA          NA
   2  2000-01-04      0.049        NA         0.049
   3  2000-01-05      0.047      -0.031        NA
   4   .
   5   .
   6   .

The code until now looks like this:
test <- read.csv("data.csv")
date <- as.Date(test$Date, format = "%m/%d/%Y")
data <- cbind(date, test[,-1]) 

When i try to apply the Return.calculate function this happens:
returns <- Return.calculate(data, method = 'log')
'Error in checkData(prices, method = 'xts'): the data cannot be converted into a time series. If you are trying to pass in names from a data object with one column, you should use the form 'data[rows, columns, drop = FALSE]'. Rownames should have standard date formats, such as '1985-03-15'.'

And when i try to convert data into an xts object this shows up:
xtsdata <- xts(data, order.by = data$date)
'Error in xts(data, order.by = data$date): 'order.by' cannot contain 'NA', 'NaN', or 'Inf'.' 

I don't understand this, since my date column is type date and my price columns are type numeric. In short, i think my problems could be solved if i could just convert the table to an xts object. Does anybody have an idea what the issue with the table could be? I am grateful for every tip, have a good one :)

A:

Assuming that what you have is as in the Note at the end either of these work:
library(xts)

# 1
z <- read.csv.zoo("data.csv", format = "%m/%d/%Y")
x <- as.xts(z)

# 2    
DF <- read.csv("data.csv")
z <- read.zoo(DF, format = "%m/%d/%Y")
x <- as.xts(z)

# Calculate returns

library(PerformanceAnalytics)

Return.calculate(x, method = 'log')
##               TickerA     TickerB    TickerC
## 2000-01-03         NA          NA         NA
## 2000-01-04 0.04879016          NA 0.04879016
## 2000-01-05 0.04652002 -0.03077166         NA

Note
Lines <- "date,TickerA,TickerB,TickerC
01/03/2000,20,,40
01/04/2000,21,33,42
01/05/2000,22,32,
"
cat(Lines, file = "data.csv")

