A data.frame object to convert an xts to R object

I would like to convert my csv files to xts objects as efficiently as possible. I seem to be stuck, although I had to first use the read.zoo method to create the zoo objects before it could convert it to an xts object.

gold <- read.zoo("GOLD.CSV", sep=",", format="%m/%d/%Y", header=TRUE) Gold <- as.xts (gold, order.by=index(gold), frequency=NULL) 

Is this the most efficient way to convert my GOLD.CSV source file to an R xts object?

+4
source share
2 answers

If this is a file, you need to read it.

So use read.zoo() like you, but then convert directly:

  gold <- as.xts(read.zoo("GOLD.CSV", sep=",", format="%m/%d/%Y", header=TRUE)) 

Ok

+3
source

You can write your own read.xts function. We will call it a wrapper, and it should go something along the lines

 read.xts <- function(x, format = "%m/%d/%Y", header = TRUE, sep = ",") { result <- as.xts(read.zoo(x, sep = sep, format = format, header = header)) return(result) } read.xts(file.choose()) # select your file 

Pay attention to the arguments in function() . They are passed to the function body (code between curly braces). If the arguments to function() have values, this means that this is their default value. If you assign new values ​​(for example, function(x = "my.file.csv", sep = "\t") ), they will overwrite the default values. The last line shows how you can use your new function. Feel free to extend this function to the rest of the read.zoo arguments. If you have any specific question on how to do this, feel free to just ask. :)

In my daily work I use several small gems. I created a file called workhorse.R and I upload it (for example, source("d:/workspace/workhorse.R") ) whenever I need any of the small functions.

+2
source

All Articles