为什么我的for循环不是每次都重新分配数据帧?

时间:2019-09-11 02:31:22

标签: r

我正在尝试从列表中筛选出的股票,这些股票没有满足一定水平的正趋势存在的条件。为此,我要求时间与股价之间的相关系数至少为0.4,这意味着应该有一定程度的正趋势。

我将for循环与if else语句组合在一起,以便检查列表中每只股票的相关性。我的目标是,如果股票的相关性> .4,它将保留在列表中,否则我将过滤到新的数据框Stock_list并过滤数据,以便除拒绝的股票以外的所有报价都保持不变。但是,我发现的是即使满足else条件,也不会应用筛选器。此外,我添加到else条件的第二部分要求其打印“已拒绝”,该部分已成功运行。我真的对为什么发生此问题感到困惑。我将google.drive链接附加到正在使用的CSV上。空间很小-仅约400行和6列。

https://drive.google.com/file/d/1h4E6yg9U6oDeKUvsWhsQbFlxL3a6Fx-K/view?usp=sharing

我已经以我可以想象的所有方式来调整了代码(虽然不多,但仍然花费了相当长的时间),但是我却无法使它正常工作。我不知道为什么没有在循环的每次迭代中应用过滤器。我正在使用Quantmod提取历史库存数据。

library(tidyverse)
library(quantmod)


    Stocks <- read_csv("2018-19-KWHS-Approved-Securities.csv")
    Stocks <- filter(Stocks, EXCHANGE == "New York Stock Exchange" |  EXCHANGE == "NASDAQ Stock Market" | EXCHANGE == "NASDAQ - Industrial" | EXCHANGE == "NASDAQ Bank - Insurance" | EXCHANGE == "NYSE MKT LLC")
    Stocks <- read_csv("2018-19-KWHS-Approved-Securities.csv")
Stocks <- filter(Stocks, EXCHANGE == "New York Stock Exchange" |  EXCHANGE == "NASDAQ Stock Market" | EXCHANGE == "NASDAQ - Industrial" | EXCHANGE == "NASDAQ Bank - Insurance" | EXCHANGE == "NYSE MKT LLC")
Stocks <- filter(Stocks, TICKER != "AMCN" & TICKER != "CHKE" & TICKER != "DFBG" & TICKER != "GPIC" & TICKER != "NTRI" & TICKER != "BORN" & TICKER != "WMIH" & TICKER != "DWDP" & TICKER != "TAHO" & TICKER != "LABL" & TICKER != "SMI " & TICKER != "UTSI" & TICKER != "SMI")

    Stock_List <- Stocks


    Stock_filter <- function (na.rm = TRUE) {


      for (i in seq_along(Stocks$TICKER)){
    z <- getSymbols.yahoo(Stocks$TICKER[i],
                          globalenv(),
                          index.class = 'Date',
                          fr0m = "2018-01-01",
                          to = Sys.Date(), header = TRUE,
                          periodicity = "daily",
                          curl.options = list(),  auto.assign = FALSE)
    z <- data.frame(date=index(z), coredata(z))  
    names(z)[5]<- "Close_Price" 
    z <- transform(z, date = as.numeric(date))

    x <<- Stocks$TICKER[i]
    if (cor(z$Close_Price, z$date) > .4) {print(x)}
    else {Stock_List <<- Stocks %>% filter(TICKER != x)
    print("Rejected")}
    }}

我已经将数据集调整为无错误,但是如前所述,我希望过滤器在满足else条件的每次迭代中发生,但那不会发生。

1 个答案:

答案 0 :(得分:0)

当我添加以下代码行时,它会运行并给出结果,但有错误...您将得到什么?

Stock_filter(Stock_List)

WARNING: There have been significant changes to Yahoo Finance data.
Please see the Warning section of ‘?getSymbols.yahoo’ for details.

This message is shown once per session and may be disabled by setting
options("getSymbols.yahoo.warning"=FALSE).
[1] "Rejected"
[1] "Rejected"
[1] "AMZN"
[1] "Rejected"
[1] "AOBC"
[1] "APTV"
[1] "Rejected"
[1] "Rejected"
[1] "Rejected"
[1] "BBY"

... ...

[1] "MSFT"
[1] "NTES"
[1] "ORCL"
[1] "SINA"
[1] "Rejected"
[1] "Rejected"
[1] "Rejected"
[1] "Rejected"
Error in if (cor(z$Close_Price, z$date) > 0.4) { : 
  missing value where TRUE/FALSE needed
In addition: Warning message:
UTSI contains missing values. Some functions will not work if objects contain missing values in the middle of the series. Consider using na.omit(), na.approx(), na.fill(), etc to remove or replace them. 
>