[R-SIG-Finance] how to grow XTS series in R dynamically ? And Quickly!

Daniel Cegiełka d@n|e|@ceg|e|k@ @end|ng |rom gm@||@com
Fri Sep 6 17:08:39 CEST 2019



> Wiadomość napisana przez Daniel Cegiełka <daniel.cegielka using gmail.com> w dniu 06.09.2019, o godz. 16:10:
> 

> 
> 2) preallocation
> 
> preallocate_matrix <- function(n)
> {
>     x <- matrix()
>     length(x) <- 4 * n      # bid, ask, bid_size, ask_size
>     dim(x) <- c(n, 4)       # see: ?dim
>     return(x)
> }
> 
> > x <- preallocate_matrix(5)
> > x
>      [,1] [,2] [,3] [,4]
> [1,]   NA   NA   NA   NA
> [2,]   NA   NA   NA   NA
> [3,]   NA   NA   NA   NA
> [4,]   NA   NA   NA   NA
> [5,]   NA   NA   NA   NA

?matrix

Usage
matrix(data = NA, nrow = 1, ncol = 1, byrow = FALSE,
       dimnames = NULL)

so we don't even need preallocate_matrix() function

> x <- .xts(matrix(nrow = 5, ncol = 4), index = Sys.time() + 1:5)
> x
                    [,1] [,2] [,3] [,4]
2019-09-06 17:07:27   NA   NA   NA   NA
2019-09-06 17:07:28   NA   NA   NA   NA
2019-09-06 17:07:29   NA   NA   NA   NA
2019-09-06 17:07:30   NA   NA   NA   NA
2019-09-06 17:07:31   NA   NA   NA   NA




-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 833 bytes
Desc: Message signed with OpenPGP
URL: <https://stat.ethz.ch/pipermail/r-sig-finance/attachments/20190906/4ebbe7ce/attachment.sig>


More information about the R-SIG-Finance mailing list