【问题标题】:cbind or merge by pasting names?通过粘贴名称来绑定或合并?
【发布时间】:2014-07-03 07:19:19
【问题描述】:

我的工作区中有许多大的xts 对象,我试图通过使用cbindmergeapply 甚至在循环中组合它们。目前xts数据为tick数据,因长度原因无法粘贴到此处。但是,我想做的是:

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

通过在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])) 

我也试过apply

apply(t(stock1), 2, cbind)

但它只组合了stock1 中的名称。我也尝试过使用:

merge(SPY.A, source [stock1])

但得到以下错误: Error in source[stock1] : object of type 'closure' is not subsettable

由于我不能把所有的报价数据都放在这里,我将提供一些代码来使用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])

【问题讨论】:

    标签: r merge xts cbind


    【解决方案1】:

    也许mgetdo.call 的组合对您有用,但如果没有一些示例输入和预期输出,就很难判断。

    一个例子:

    ## 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
    

    【讨论】:

    • 这正是我想要的。我以前从未使用过mget。谢谢!
    • @Rime,很高兴听到这个消息。我看到你已经用一个可重复的例子更新了你的问题,所以 +1 为那里的努力:-)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-04-17
    • 2020-09-01
    • 1970-01-01
    • 2011-12-23
    • 1970-01-01
    • 1970-01-01
    • 2023-04-04
    相关资源
    最近更新 更多