Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
210 views
in Technique[技术] by (71.8m points)

r - Unable to understand how list is built

Can someone explain me why the code line below :

Returns <- eapply(Data,function(s) ROC(Ad(s), type="discrete"))

does return :

enter image description here

I guess that it's classed by value but quite not sure

Since Data is built as below :

enter image description here

Why isn't my list structured as excepted :

Returns : 
  -> ACA.PA
  -> BNP.PA
  -> UG.PA 

How would I Fix this in order to keep the same structure ?

Reproducible Example

stock_list <- c("ACA.PA","BNP.PA","UG.PA")
Data <- new.env(hash = FALSE)
getSymbols(stock_list,
           from = start_date,
           to = end_date,
           src = "yahoo",
           periodicity = "monthly",
           env=Data)
Returns <- lapply(Data,function(s) ROC(Ad(s), type="discrete"))

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

if we need the output in the same order, we can use mget with envir specified as 'Data'

Returns <- lapply(mget(stock_list, envir = Data), 
        function(s) ROC(Ad(s), type="discrete"))

names(Returns)
#[1] "ACA.PA" "BNP.PA" "UG.PA" 

According to ?eapply

order of the components is arbitrary for hashed environments.

and in ?new.env

env.profile returns a list with the following components: size the number of chains that can be stored in the hash table, nchains the number of non-empty chains in the table (as reported by HASHPRI), and counts an integer vector giving the length of each chain (zero for empty chains). This function is intended to assess the performance of hashed environments. When env is a non-hashed environment, NULL is returned.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...