Search code examples
rxtszooimport-from-exceldata-import

Quotation marks when zoo to xts using as.xts() in R


When transforming the below data (class "zoo") into xts, I use returns.xts<-as.xts(returns). The effect is to add quotation marks around the new Data. And then becomes unusable in functions.

Why is this?

class("zoo")

           UK.EQUITY    EUR.EQUITY   NA.EQUITY    ASIA.EQUITY  JPN.EQUITY   EM.EQUITY    WORLD.EQUITY.EX.UK
2006-04-30 0.010552982  -0.003337229 -0.033739353 0.025092643  -0.020920633 0.020016007  -0.021165353      
2006-05-31 -0.048962517 -0.071924029 -0.059684763 -0.102475485 -0.098121902 -0.141877111 -0.067793295    

class ("zoo" "xts")

           UK.EQUITY      EUR.EQUITY     NA.EQUITY      ASIA.EQUITY    JPN.EQUITY     EM.EQUITY     
2006-04-30 "0.010552982"  "-0.003337229" "-0.033739353" "0.025092643"  "-0.020920633" "0.020016007" 
2006-05-31 "-0.048962517" "-0.071924029" "-0.059684763" "-0.102475485" "-0.098121902" "-0.141877111"

Solution

  • xts is not "adding quotation marks". xts prints character data with quotation marks, but zoo does not. str(returns) and str(as.xts(returns)) should both show that the coredata of the objects is character. This is because "#N/A" cannot be converted to a number.

    You don't say how you're reading from Excel (though the tags you added suggest you are), but there are usually ways to specify how NA values are represented. For example, read.csv has a na.strings argument you can set to "#N/A".