get xts objects from within an environment

后端 未结 2 1324
温柔的废话
温柔的废话 2021-01-13 21:38

I have stored xts objects inside an environment. Can I subset these objects while they are stored in an environment, i.e. act upon them \"in-place\"? Can I extract these obj

相关标签:
2条回答
  • 2021-01-13 22:25

    This will subset every object in an environment, and return an environment with the subsetted data:

    data2 <- as.environment(eapply(data, "[", paste(date.start, date.end, sep="/")))
    

    You can do basically the same thing for your second question. Just, name the components of the list that lapply returns by wrapping it with setNames, then coerce to an environment:

    data3 <- as.environment(setNames(lapply(tickers, get, envir = data), tickers))
    

    Or, better yet, use mget so that you don't have to use lapply or setNames

    data3 <- as.environment(mget(tickers, envir = data))
    

    Alternatively I actually have a couple convenience functions in qmao designed specifically for this: gaa stands for "get, apply, assign" and gsa stands for "get, subset, assign".

    To, get data for some tickers, subset the data, and then assign into an environment

    gsa(tickers, subset=paste(date.start, date.end, sep="/"), env=data, 
        store.to=globalenv())
    

    gaa lets you apply any function to each object before saving in the same or different environment.

    0 讨论(0)
  • 2021-01-13 22:31

    If I'm reading the question correctly, you want smth like this:

    dtxl = do.call(cbind, sapply(tickers2,
               function(ticker) get(ticker, env=data)[paste(date.start,date.end,sep="/")])
           )
    
    0 讨论(0)
提交回复
热议问题