Home > database >  Merging many lists of different XTS objects in R
Merging many lists of different XTS objects in R

Time:12-26

I have 3 lists of large XTS objects: "SMA"; "L", "Marubozu". Quick look how it looks:

> names(Marubozu)
 [1] "TSLA"  "AAPL"  "NTES"  "GOOGL" "ASML"  "GOOG"  "NFLX"  "ADBE"  "AMZN"  "MSFT"  "ADI"   "FB"   
> names(SMA)
 [1] "TSLA"  "AAPL"  "NTES"  "GOOGL" "ASML"  "GOOG"  "NFLX"  "ADBE"  "AMZN"  "MSFT"  "ADI"   "FB"   
> names(L)
 [1] "TSLA"  "AAPL"  "NTES"  "GOOGL" "ASML"  "GOOG"  "NFLX"  "ADBE"  "AMZN"  "MSFT"  "ADI"   "FB"

> head(Marubozu$AAPL, n = 2)
           WhiteMarubozu BlackMarubozu
2000-01-03         FALSE         FALSE
2000-01-04         FALSE         FALSE
> head(SMA$AAPL, n = 2)
           UpTrend NoTrend DownTrend Trend
2000-01-03      NA      NA        NA    NA
2000-01-04      NA      NA        NA    NA
> head(L$AAPL, n =2)
           AAPL.Open AAPL.High AAPL.Low AAPL.Close AAPL.Volume AAPL.Adjusted
2000-01-03  0.936384  1.004464 0.907924   0.999442   535796800      0.856887
2000-01-04  0.966518  0.987723 0.903460   0.915179   512377600      0.784643   

I want to merge corresponding XTS objects in that lists so that it creates one big lig list. For example, the output for New_List$AAPL would be:

AAPL.Open AAPL.High AAPL.Low AAPL.Close AAPL.Volume AAPL.Adjusted WhiteMarubozu BlackMarubozu UpTrend NoTrend DownTrend Trend
2000-01-03  0.936384  1.004464 0.907924   0.999442   535796800      0.856887             0             0      NA      NA        NA    NA
2000-01-04  0.966518  0.987723 0.903460   0.915179   512377600      0.784643             0             0      NA      NA        NA    NA

I tried to create a list of lists and merging it, but it didnt work. Here you can see:

#That works for a single ticker AAPL      
full <- merge.xts(L$AAPL, Marubozu$AAPL, SMA$AAPL)

#This doesn't work
out3 <- Map(function(x) {full$x <- merge.xts(lista[[1]]$x, lista[[2]]$x)}, lista)

I guess it is just some simple 2-lines thing but can't really find the solution, thanks for any responses!

CodePudding user response:

We could do this with Map - as the list of xts elements have the same tickers in the same order, just use Map instead of creating a list of lists

library(xts)
out <- Map(merge.xts, L, Marubozu, SMA)

CodePudding user response:

Here's a small function u() that binds the xts-index to an xts object and converts to 'data.frame'.

u <- function(x) cbind.data.frame(index=index(x), unclass(x))

To test it, we create some data using sample_matrix which comes with xts. We split first two and last two columns into two separate xts objects with same index.

library(xts)
data(sample_matrix)
sample.xts <- as.xts(sample_matrix, descr='my new xts object')

S1 <- as.xts(sample_matrix[,1:2])  ##
S2 <- as.xts(sample_matrix[,3:4])

Now we may easily apply merge and create a new xts object out of it.

res <- merge(u(S1), u(S2)) |>
  (\(x) xts(x[-1], x$index, descr='my new xts object'))()

class(res)
# [1] "xts" "zoo"

stopifnot(all.equal(res, sample.xts))  ## proof
  • Related