Importare e trasformare più strumenti

Importare e gestire dati finanziari in R

Joshua Ulrich

Quantitative Analyst & quantmod Co-Author and Maintainer

Scarica gli strumenti in un ambiente personalizzato

# Create new environment
data_env <- new.env()

# Use getSymbols to load data into the environment getSymbols(c("SPY", "QQQ"), env = data_env, auto.assign = TRUE)
"SPY" "QQQ"
# Look at a few rows of the SPY data
head(data_env$SPY, 3)
            SPY.Open SPY.High SPY.Low SPY.Close SPY.Volume SPY.Adjusted
 2007-01-03   142.25   142.86  140.57    141.37   94807600     114.8094
 2007-01-04   141.23   142.05  140.61    141.67   69620600     115.0530
 2007-01-05   141.33   141.40  140.38    140.54   76645300     114.1353
Importare e gestire dati finanziari in R

Uso di lapply()

  • Scorre tutti gli oggetti nell’ambiente
  • Combina l’elenco dei risultati in un unico oggetto con do.call()
    • Primo argomento (what): funzione da chiamare
    • Secondo argomento (args): lista di argomenti da passare
Importare e gestire dati finanziari in R

Estrai l’Adjusted e unisci in un unico oggetto

# Extract volume column from each object
adjusted_list <- lapply(data_env, Ad)

# Merge each list element into one object adjusted <- do.call(merge, adjusted_list)
head(adjusted)
            QQQ.Adjusted SPY.Adjusted
 2007-01-03     39.47694     114.8094
 2007-01-04     40.22558     115.0530
 2007-01-05     40.03385     114.1353
 2007-01-08     40.06124     114.6632
 2007-01-09     40.26210     114.5658
 2007-01-10     40.73684     114.9475
Importare e gestire dati finanziari in R

Facciamo pratica!

Importare e gestire dati finanziari in R

Preparing Video For Download...