do_call: Recursively apply a function to a list of objects, such as...

View source: R/rutils.R

do_callR Documentation

Recursively apply a function to a list of objects, such as xts time series.

Description

Performs a similar operation as do.call(), but using recursion, which is much faster and uses less memory. The function do_call() is a generalization of function do_call_rbind().

Usage

do_call(func, listv, ...)

Arguments

func

The name of function that returns a single object from a list of objects.

listv

A list of objects, such as vectors, matrices, data frames, or time series.

...

Additional arguments to function func().

Details

The function do_call() performs an lapply loop, each time binding neighboring elements and dividing the length of listv by half. The result of performing do_call(rbind, list_xts) on a list of xts time series is identical to performing do.call(rbind, list_xts). But do.call(rbind, list_xts) is very slow, and often causes an ‘out of memory’ error.

Value

A single vector, matrix, data frame, or time series.

Examples

# Create xts time series
xtsv <- xts(x=rnorm(1000), order.by=(Sys.time()-3600*(1:1000)))
# Split time series into daily list
list_xts <- split(xtsv, "days")
# rbind the list back into a time series and compare with the original
identical(xtsv, rutils::do_call(rbind, list_xts))

algoquant/rutils documentation built on April 18, 2024, 12:05 a.m.