首页 > 解决方案 > 将环境中的特定元素列与 for 循环组合

问题描述

我已将 S&P500 医疗保健股票的股票数据下载到一个单独的环境中,现在我想从环境的每个元素中获取 Adjusted close 列并将它们组合到一个数据框中。我写了以下内容,但出现错误:

   require(quantmod)
require(PerformanceAnalytics)
require(PortfolioAnalytics)

#environment where stocks data will be saved
stocks_environment <- new.env()

#day to start downloading the data from
start_date <- "2020-01-01"

#create vectors containing the sector relevant tickers from the S&P500 and download the data
health_care_sector <- c("A",    "ABBV", "ABC",  "ABMD", "ABT",  "ALGN", "ALXN", "AMGN", "ANTM", "BAX",  "BDX",  "BIIB", "BMY",  "BSX",  "CAH",  "CERN", "CI",   "CNC",  "COO",  "CVS",  "DGX",  "DHR",  "DVA",  "EW",   "GILD", "HCA",  "HOLX", "HSIC", "HUM",  "IDXX", "ILMN", "INCY", "IQV",  "ISRG", "JNJ",  "LH",   "LLY",  "MCK",  "MDT",  "MRK",  "MTD",  "MYL",  "PFE",  "PKI",  "PRGO", "REGN", "RMD",  "STE",  "SYK",  "TFX",  "TMO",  "UHS",  "UNH",  "VAR",  "VRTX", "WAT",  "XRAY", "ZBH",  "ZTS")
health_care_sector_names <- c("Agilent Technologies",   "Abbvie Inc",   "Amerisourcebergen Corp",   "Abiomed Inc",  "Abbott Laboratories",  "Align Technology", "Alexion Pharm Inc",    "Amgen Inc",    "Anthem Inc",   "Baxter International Inc", "Becton Dickinson and Company", "Biogen Inc",   "Bristol-Myers Squibb Company", "Boston Scientific Corp",   "Cardinal Health",  "Cerner Corp",  "Cigna Corp",   "Centene Corp", "Cooper Companies", "CVS Corp", "Quest Diagnostics Inc",    "Danaher Corp", "Davita Healthcare Partners Inc",   "Edwards Lifesciences Corp",    "Gilead Sciences Inc",  "Hca Holdings Inc", "Hologic Inc",  "Henry Schein Inc", "Humana Inc",   "Idexx Laboratories",   "Illumina Inc", "Incyte Corp",  "Iqvia Holdings Inc",   "Intuitive Surg Inc",   "Johnson & Johnson",    "Laboratory Corp of America Holdings",  "Eli Lilly and Company",    "Mckesson Corp",    "Medtronic Inc",    "Merck & Company",  "Mettler-Toledo International", "Mylan NV Ord Shs", "Pfizer Inc",   "Perkinelmer",  "Perrigo Company",  "Regeneron Pharmaceuticals",    "Resmed Inc",   "Steris Corp",  "Stryker Corp", "Teleflex Inc", "Thermo Fisher Scientific Inc", "Universal Health Services",    "Unitedhealth Group Inc",   "Varian Medical Systems",   "Vertex Pharmaceutic",  "Waters Corp",  "Dentsply Sirona Inc",  "Zimmer Biomet Holdings",   "Zoetis Inc Cl A")
getSymbols(health_care_sector,from = start_date,env = stocks_environment)


number_of_rows <- nrow(stocks_environment$A)
number_of_columns <- length(health_care_sector)
health_care_matrix <- matrix(nrow = number_of_rows,ncol = number_of_columns)
colnames(health_care_matrix) <- health_care_sector_names

#combine all the adjusted closing prices for each stock in the stockenvironment into one matrix 
for(i in health_care_sector){
  x <- get(i,envir = stocks_environment)[,6]["2020/"]
  start <- 1
  health_care_matrix[,start] <- x
  start <- start+1
}

在进入 for 循环之前,我没有收到错误消息。

标签: rfor-loop

解决方案


请注意,您在 for 循环中有启动计数器,将计数器重置为 1。修复该问题后代码工作。

start <- 1
for(i in health_care_sector){
  x <- get(i,envir = stocks_environment)[,6]["2020/"]
  print(head(x))
  health_care_matrix[,start] <- x
  start <- start+1
}

推荐阅读