Rime
Rime

Reputation: 942

cbind or merge by pasting names?

I have many large xts objects in my workspace that i am trying to combine by using cbind , merge, apply or even in a loop. Currently, the xts data is tick data and cannot paste it here because of its length. However, what I am trying to do is:

cbind(AAPL.O, AMZN.O, BIDU.O, GLD.A, ...) -> all

by pasting using the following names on cbind

stock1 <- c("AAPL.O", "AMZN.O", "BIDU.O", "GLD.A", ...) # names of xts objects

# However this only combines the names "AAPL.O" & "AMZN.O"
cbind(paste(stock1[1]), paste(stock1[2])) 

I have also tried apply :

apply(t(stock1), 2, cbind)

but it only combines the names as in stock1. I have also tried using:

merge(SPY.A, source [stock1])

but get the following error: Error in source[stock1] : object of type 'closure' is not subsettable

Since I can't put all the tick data in here I will provide some code to download data from online using getSymbols()

library(quantmod)

symbols <- c("AAPL", "AMZN", "BIDU", "GLD")

getSymbols(symbols)

#These will yield the same problem I am having
cbind(paste(symbols[1]),paste(symbols[2] ))  

apply(t(symbols), 2, cbind)

merge(AAPL, source [symbols])

Upvotes: 1

Views: 946

Answers (1)

A5C1D2H2I1M1N2O1R2T1
A5C1D2H2I1M1N2O1R2T1

Reputation: 193657

Maybe a combination of mget and do.call would work for you, but it's hard to tell without some sample input and expected output.

An example:

## Some matrices to combine using `cbind`
M1 <- matrix(1:4, ncol = 2)
M2 <- matrix(5:8, ncol = 2)
M3 <- matrix(9:10, ncol = 1)

## Like your "stock1"
Get <- c("M1", "M2", "M3")

do.call(cbind, mget(Get))
#      [,1] [,2] [,3] [,4] [,5]
# [1,]    1    3    5    7    9
# [2,]    2    4    6    8   10

Upvotes: 1

Related Questions