如何将 Cl() 与字符一起使用?

How to use Cl() with character?

我在 csv 文件中有一个符号列表,我想筛选收盘价高于 5 的符号。

我已经 运行 获取 csv 文件中所有项目的符号。

我的测试在这里出错了。我认为这是因为 Cl() 不接受字符作为参数。如何将字符转换为 xts?

> library(quantmod)

> passingset
         V1
1       AAB
2    AAR-UN
3       AAV
4       ABT
5       ABX
       (...)

> class(passingset)
[1] "data.frame"
> class(passingset$V1)
[1] "character"


> #Remove closing price < 5
> for (i in 1:nrow(passingset)){
+   company <- passingset$V1[i]
+   if(Cl(company)[length(Cl(company))] < 5){
+     passingset <- passingset[!(passingset$V1 == company),, drop = FALSE]
+   } else 
+       i = i + 1
+   
+   rm(company)
+ }
**Error in Cl(company) : 
  subscript out of bounds: no column name containing "Close"**

无需循环! 假设您已经阅读了上面提到的带有 getSymbols 的价格系列,那么:

# an example set of tickers:
pasingset <- data.frame(V1=c('CWB','EEM','VTI','NAK','GIG'),stringsAsFactors = F)

# this gets you the last closings of your tickers
lastClose <- sapply(pasingset$V1,function(x) xts::last(Cl(get(x))))

# shows the tickers which have a close > 5
pasingset$V1[which(lastClose > 5)]
[1] "CWB" "EEM" "VTI"