0

The following code renders a certain response to a query:

library(quantmod)

# Load ticker data from 2020-01-01 till 2021-02-02
t <- c("NKLA", "MPNGF", "RMO", "JD", "MSFT")
getSymbols.yahoo(t, auto.assign = TRUE, env = globalenv(), 
                 from = "2020-01-01", to = "2021-02-02")

# Close all Internet connections as a precaution
closeAllConnections()

# Find xts objects
x <- names(which(unlist(eapply(.GlobalEnv, is.xts))))

# Convert xts to data.frame
for (i in seq_along(x)) {
  assign(x[i], fortify.zoo(get(x[i])))
}

# The query
sapply(mget(x), names)

# The rendering
     NKLA            MPNGF            MSFT            JD            RMO           
[1,] "Index"         "Index"          "Index"         "Index"       "Index"       
[2,] "NKLA.Open"     "MPNGF.Open"     "MSFT.Open"     "JD.Open"     "RMO.Open"    
[3,] "NKLA.High"     "MPNGF.High"     "MSFT.High"     "JD.High"     "RMO.High"    
[4,] "NKLA.Low"      "MPNGF.Low"      "MSFT.Low"      "JD.Low"      "RMO.Low"     
[5,] "NKLA.Close"    "MPNGF.Close"    "MSFT.Close"    "JD.Close"    "RMO.Close"   
[6,] "NKLA.Volume"   "MPNGF.Volume"   "MSFT.Volume"   "JD.Volume"   "RMO.Volume"  
[7,] "NKLA.Adjusted" "MPNGF.Adjusted" "MSFT.Adjusted" "JD.Adjusted" "RMO.Adjusted"

The same code adjusted to fit in a specific environment:

library(quantmod)

symbolUpdates.env <- new.env()

# Load ticker data from 2020-01-01 till 2021-02-02 to symbolUpdates.env
t2 <- c("NKLA", "MPNGF", "RMO", "JD", "MSFT")
getSymbols.yahoo(t2, auto.assign = TRUE, env = symbolUpdates.env, 
                 from = "2020-01-01", to = "2021-02-02")

# Close all Internet connections as a precaution
closeAllConnections()

# Find xts objects in symbolUpdates.env
x2 <- names(which(unlist(eapply(symbolUpdates.env, is.xts))))

# Convert xts to data.frame that are in symbolUpdates.env
for (i2 in seq_along(x2)) {
  assign(envir = symbolUpdates.env, x2[i], fortify.zoo(get(x2[i2])))
}

# The query in symbolUpdates.env
sapply(mget(x2, envir = symbolUpdates.env), names)

# The rendering from symbolUpdates.env
     RMO            NKLA            JD            MSFT            MPNGF           
[1,] "Index"        "Index"         "Index"       "Index"         "Index"         
[2,] "Index"        "Index"         "Index"       "Index"         "Index"         
[3,] "RMO.Open"     "NKLA.Open"     "JD.Open"     "MSFT.Open"     "MPNGF.Open"    
[4,] "RMO.High"     "NKLA.High"     "JD.High"     "MSFT.High"     "MPNGF.High"    
[5,] "RMO.Low"      "NKLA.Low"      "JD.Low"      "MSFT.Low"      "MPNGF.Low"     
[6,] "RMO.Close"    "NKLA.Close"    "JD.Close"    "MSFT.Close"    "MPNGF.Close"   
[7,] "RMO.Volume"   "NKLA.Volume"   "JD.Volume"   "MSFT.Volume"   "MPNGF.Volume"  
[8,] "RMO.Adjusted" "NKLA.Adjusted" "JD.Adjusted" "MSFT.Adjusted" "MPNGF.Adjusted"

My questions:

  • Are there any mistakes in the code that an additional Index column appears in symbolUpdates.env?
  • If yes, what are they?
  • What could correct the issue?
  • Also, the order of the tickers is not respected in symbolUpdates.env, why?

Thanks in advance.


Systems used:

  • R version: 4.1.1 (2021-08-10)
  • RStudio version: 1.4.1717
  • OS: macOS Catalina version 10.15.7 and macOS Big Sur version 11.6
user438383
  • 5,716
  • 8
  • 28
  • 43
pdeli
  • 436
  • 3
  • 13
  • Wouldn't it be much much easier and cleaner to just use (named)lists instead of `assign` and environments ? – dario Oct 08 '21 at 11:33
  • One error in your second version is in the `get()` call: `get(x2[i]))` defaults to the global environment, not `symbolUpdates.env`. – user2554330 Oct 08 '21 at 12:17
  • @user2554330, thanks for your comment. I corrected the mistake in the post. I did it in ```R``` too, but the problem persists: I still have 2 ```Index``` columns. – pdeli Oct 08 '21 at 12:52
  • @dario. It is possible, however I have no idea how to do that. Would you mind showing how? – pdeli Oct 08 '21 at 12:53
  • That is how we assign a value to named list in R: `new_list <- list(); new_list[["new_element"]] <- "new_element_value"`. – dario Oct 08 '21 at 13:04
  • If you have further questions I'd suggest crating a [minimal reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/5963610#5963610). For example, if the question does not depend on the quantmod package remove it (I for one dont have that installed, so I won't be able to help) – dario Oct 08 '21 at 13:06
  • @pdeli, you still have an error in the `get()`. Specifically, if you want to get something from a particular environment, you need to specify the `envir` argument. – user2554330 Oct 08 '21 at 13:56
  • @user2554330, thank you!M That did it. So ```for (i2 in seq_along(x2)) { assign(envir = symbolUpdates.env, x2[i2], fortify.zoo(get(x2[i]))) }``` should have been ```for (i2 in seq_along(x2)) { assign(envir = symbolUpdates.env, x2[i2], fortify.zoo(get(x2[i2], envir = symbolUpdates.env))) }```. Two errors in the line… – pdeli Oct 08 '21 at 17:03

2 Answers2

0

The order can be arranged with match and for me I don't get any additional Index column.

lapply(mget(x2, envir = symbolUpdates.env), names)
data <- mget(x2[match(t2, x2)], envir = symbolUpdates.env)
sapply(data, names)

#      NKLA            MPNGF            RMO            JD            MSFT           
#[1,] "NKLA.Open"     "MPNGF.Open"     "RMO.Open"     "JD.Open"     "MSFT.Open"    
#[2,] "NKLA.High"     "MPNGF.High"     "RMO.High"     "JD.High"     "MSFT.High"    
#[3,] "NKLA.Low"      "MPNGF.Low"      "RMO.Low"      "JD.Low"      "MSFT.Low"     
#[4,] "NKLA.Close"    "MPNGF.Close"    "RMO.Close"    "JD.Close"    "MSFT.Close"   
#[5,] "NKLA.Volume"   "MPNGF.Volume"   "RMO.Volume"   "JD.Volume"   "MSFT.Volume"  
#[6,] "NKLA.Adjusted" "MPNGF.Adjusted" "RMO.Adjusted" "JD.Adjusted" "MSFT.Adjusted"
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
  • What I think is needed (not sure if this is true/necessary) is that the order of the content of ```symbolUpdates.env``` match the order of the content of ```.GlobalEnv``` (i.e., ```symbolUpdates.env``` content order = ```RMO NKLA JD MSFT MPNGF``` , whereas ```.GlobalEnv``` content order = ```MSFT MPNGF NKLA JD RMO```). – pdeli Oct 09 '21 at 16:58
  • Nevertheless, ```identical(names(x), names(x2))``` renders ```TRUE```, so I guess ```data.frame``` objects order in different ```environment```s should not be a problem to append one to the other. Am I right? However, ```identical(x, x2)``` renders ```FALSE```… – pdeli Oct 09 '21 at 17:20
0

@user2554330 pointed to the coding error and a piece missing. Correcting the first and adding the second did the trick.

Here is how the original code of the last for loop looked like:

for (i2 in seq_along(x2)) {
  assign(envir = symbolUpdates.env, x2[i], fortify.zoo(get(x2[i])))
}

Here is how it should have looked like (i2 instead of i and adding envir = symbolUpdates.env at the end:

for (i2 in seq_along(x2)) {
  assign(envir = symbolUpdates.env, x2[i2], fortify.zoo(get(x2[i2], 
                                           envir = symbolUpdates.env)))
}
pdeli
  • 436
  • 3
  • 13