regex - building a large xts object out of a list of files with rbind.xts and a user defined function -
i trying create 1 large xts data set out of list of files. able accomplish being explicit shown below, read.cqg
user defined function using read.zoo
, as.xts
read/convert data xts object. each read.cqg
call returns xts object.
the code below works , returns large xts object.
large_xts_object <- rbind.xts(read.cqg("somefile01.txt"), read.cqg("somefile02.txt"), read.cqg("somefile03.txt"), read.cqg("somefile04.txt"), read.cqg("somefile05.txt"), read.cqg("somefile06.txt"), read.cqg("somefile07.txt"))
i use regular expressions , lapply
avoid having explicitly write file name. me understand using lapply
, shorten code.
this attempt, doesn't give me results want.
large_xts_obj <- rbind.xts(lapply(list.files(pattern="^somefile*.*txt"), read.cqg))
this returns large list of xts objects instead of 1 large xts object. how can use rbind.xts
, custom read.cqg
function, , pattern
argument of list.files
create single xts object want?
you should not call methods (e.g. rbind.xts
) directly. use generic function , let r method dispatch. aside that, code in comment correct.
do.call
allows construct , evaluate function call providing function , list of parameters. in case, list of parameters list of xts objects read via read.cqg
.
files <- list.files(pattern = "^files*.*txt") xts_object_list <- lapply(files, read.cqg) large_xts_object <- do.call(rbind, xts_object_list)
Comments
Post a Comment