从R中的Environment中提取数据或调整代码以保存为数据格式。

时间:2017-01-24 22:34:37

标签: r environment-variables tidyquant

我有以下代码: 它保存了csv文件中的历史库存数据,这正是我需要的,但是所有数据都是环境格式的,我无法使用它。是否可以调整代码,以便将信息保存为"数据"便于处理。 也许我有办法将环境转换为更友好的格式,这可以是一个解决方案。总而言之,我有我需要的信息,但我现在不知道如何使用它:)

#install.packages("quantmod")
library("quantmod")
#Script to download prices from yahoo
#and Save the prices to a RData file
#The tickers will be loaded from a csv file

#Script Parameters
tickerlist <- "sp500.csv"  #CSV containing tickers on rows
savefilename <- "stockdata.RData" #The file to save the data in
startDate = as.Date("2005-01-13") #Specify what date to get the prices from
maxretryattempts <- 5 #If there is an error downloading a price how many times to retry

#Load the list of ticker symbols from a csv, each row contains a ticker
stocksLst <- read.csv("sp500.csv", header = F, stringsAsFactors = F)
stockData <- new.env() #Make a new environment for quantmod to store data in
nrstocks = length(stocksLst[,1]) #The number of stocks to download

#Download all the stock data
for (i in 1:nrstocks){
for(t in 1:maxretryattempts){

   tryCatch(
       {
           #This is the statement to Try
           #Check to see if the variables exists
           #NEAT TRICK ON HOW TO TURN A STRING INTO A VARIABLE
           #SEE  http://www.r-bloggers.com/converting-a-string-to-a-variable-name-on-the-fly-and-vice-versa-in-r/
            if(!is.null(eval(parse(text=paste("stockData$",stocksLst[i,1],sep=""))))){
                #The variable exists so dont need to download data for this stock
                #So lets break out of the retry loop and process the next stock
                #cat("No need to retry")
                break
            }

          #The stock wasnt previously downloaded so lets attempt to download it
          cat("(",i,"/",nrstocks,") ","Downloading ", stocksLst[i,1] , "\t\t Attempt: ", t , "/", maxretryattempts,"\n")
          getSymbols(stocksLst[i,1], env = stockData, src = "yahoo", from = startDate)
       }
    #Specify the catch function, and the finally function
   , error = function(e) print(e))
 }
}

#Lets save the stock data to a data file
tryCatch(
{
save(stockData, file=savefilename)
cat("Sucessfully saved the stock data to %s",savefilename)
}
, error = function(e) print(e))

3 个答案:

答案 0 :(得分:1)

为此功能创建数据的新环境(stockData <- new.env())的过程是否典型?

您的for循环实际上并未分配任何对象,它只打印getSymbols的结果。您可以将它们存储在列表中,即

stockData[i] <- getSymbols(<stuff>)

除此之外:考虑较新的tidyquant包,它以整齐的格式(tibble)存储相同的结果:https://github.com/mdancho84/tidyquant

答案 1 :(得分:1)

设置包含IBM和MSFT的测试环境e。然后给出这些股票加上GOOG和TSLA的向量,使用setdiff排除已经e下载余数的那些:

# test data
e <- new.env()
stks <- c("IBM", "MSFT", "GOOG", "TSLA")
getSymbols(stks[1:2], env = e)

# run
rest <- setdiff(stks, ls(e))
getSymbols(rest, env = e)

答案 2 :(得分:0)

如果我理解这个问题,你最终会试图获得指数中每只股票的股票价格。这是tidyquant包的一个很好的用例,因为包已经内置了错误处理。错误作为NA返回,并在向用户提供警告消息时从结果中删除。

tidyquant软件包 v0.3.0 (当前CRAN版本)中试用:

    library(tidyquant)
    sp_500 <- tq_get("SP500", get = "stock.index") %>%
        tq_get(get = "stock.prices")

请注意,上面的代码块正在发生变化。对于不同的索引函数get = "stock.index",我们正在从tq_get()弃用tq_index()选项。这是下一个CRAN版本的内容,以及dev version中发布的内容, v0.3.0.9030

    library(tidyquant)
    sp_500 <- tq_index("SP500") %>%
        tq_get(get = "stock.prices", complete_cases = TRUE)

默认值为complete_cases = TRUE。如果您想使用NA返回值,请更改complete_cases = FALSE。请注意,如果由于符号不正确而无法检索股票价格,则会返回NA。如果NA存在,您将获得一个&#34;嵌套&#34;数据框和一条警告消息,说明哪个符号有错误。