簡體   English   中英

如何在字符中使用Cl()?

[英]How to use Cl() with character?

我在csv文件中有一個交易品種列表,我想篩選收盤價高於5的交易品種。

我已經為csv文件中的所有項目運行了getSymbols。

我的測試在這里出錯。 我認為這是因為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"

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM