Reputation: 7
I'm trying to write a function that will load some xts data into R, and sort them by the date/time I specify. Maybe I'm trying a too simplistic method, but here is my code:
load.data <- function (x, time1, time2) #x is going to be the actual file name
{
vector = get(load("C:/Users/username/Desktop/x"))
sortvector = vector['time1/time2/']
return (sortvector)
}
And when I execute it, I get the message:
In readChar(con, 5L, useBytes = TRUE) :
cannot open compressed file 'C:/Users/username/Desktop/x', probable reason 'No such file or directory'
So, how can I make it so that my function will actually search for the file name, rather than actual generic 'x'? I hope I was clear, and I would certainly appreciate any help greatly.
Upvotes: 0
Views: 681
Reputation: 49820
Use paste
or paste0
load.data <- function (x, time1, time2) #x is going to be the actual file name
{
vector = get(load(paste0("C:/Users/username/Desktop/", x)))
sortvector = vector[paste(time1, time2, sep='/')]
return(sortvector)
}
Also, look at ?FinancialInstrument:::saveSymbols.days
and ?FinancialInstrument:::getSymbols.FI
, or look at the code for those functions for examples of saving and loading xts
objects
Edit
Here's an example
set.seed(123)
dat <- xts(cumsum(rnorm(100)), as.POSIXct("2012-05-23") + 1:100*60*60*6)
tmpdir <- tempdir()
save(dat, file=file.path(tmpdir, "dat.RData"))
rm('dat')
time1 <- "2012-05-24 10:00:00"
time2 <- "2012-05-25 11:00:00"
vector = get(load(paste(tmpdir, "dat.RData", sep="/")))
sortvector = vector[paste(time1, time2, sep='/')]
sortvector
# [,1]
#2012-05-24 12:00:00 -2.044404
#2012-05-24 18:00:00 -2.829308
#2012-05-25 00:00:00 -4.497250
#2012-05-25 06:00:00 -4.877477
Upvotes: 2