通过列表的特定列值获取对象名称

huangapple go评论73阅读模式
英文:

Get object names by specific column value of a list

问题

以下是要翻译的代码部分:

library(quantmod)

f <- function(symbol, dataStartDate = as.Date("2020-04-01")) {
  
  consecutiveTruesExtractor <- function(data){
    genNumOfConsecutiveTrues <- function(x, y) { (x+y)*y  } #Y is either 0 or 1
    upDaysCount <- Reduce( genNumOfConsecutiveTrues, data, accumulate=TRUE)
    upDaysCount <- as.vector(Lag(upDaysCount))
    upDaysCount[is.na(upDaysCount)] <- 0
    
    downDaysCount <- Reduce( genNumOfConsecutiveTrues, !data, accumulate=TRUE)
    downDaysCount <- as.vector(Lag(downDaysCount))
    downDaysCount[is.na(downDaysCount)] <- 0
    consecutiveTruesExtractor <- upDaysCount-downDaysCount
  }
  
  symbolData <- new.env() #Make a new environment for quantmod to store data in
  getSymbols(symbol, env = symbolData, src = "yahoo", from = dataStartDate)
  mktdata <- eval( parse(text = paste("symbolData$", sub("^", "", symbol, fixed=TRUE))))
  opClRet <- (Cl(mktdata)/Op(mktdata))-1
  consecutiveDir <- consecutiveTruesExtractor(as.matrix(opClRet>0))
  completeData<- cbind(opClRet,consecutiveDir)
  colnames(completeData) <- c("OpClRet","ConsecutiveDir")
  
  mktdata$Consecutive <- completeData$ConsecutiveDir
  
  mktdata
}

Symbols <- c("AMD","A","AAL","ABBV","ABT","ACAD","ADBE")
             
L <- Map(f, Symbols) 


last_row_values<-lapply(L,tail,1)

希望你可以使用这段代码来获取最后一行的值,并筛选出具有特定值的股票名称。

英文:

I'm running the following code to get consecutive days of price up/down for several tickers

library(quantmod)

f &lt;- function(symbol, dataStartDate = as.Date(&quot;2020-04-01&quot;)) {
  
  consecutiveTruesExtractor &lt;- function(data){
    genNumOfConsecutiveTrues &lt;- function(x, y) { (x+y)*y  } #Y is either 0 or 1
    upDaysCount &lt;- Reduce( genNumOfConsecutiveTrues, data, accumulate=TRUE)
    upDaysCount &lt;- as.vector(Lag(upDaysCount))
    upDaysCount[is.na(upDaysCount)] &lt;- 0
    
    downDaysCount &lt;- Reduce( genNumOfConsecutiveTrues, !data, accumulate=TRUE)
    downDaysCount &lt;- as.vector(Lag(downDaysCount))
    downDaysCount[is.na(downDaysCount)] &lt;- 0
    consecutiveTruesExtractor &lt;- upDaysCount-downDaysCount
  }
  
  symbolData &lt;- new.env() #Make a new environment for quantmod to store data in
  getSymbols(symbol, env = symbolData, src = &quot;yahoo&quot;, from = dataStartDate)
  mktdata &lt;- eval( parse(text = paste(&quot;symbolData$&quot;, sub(&quot;^&quot;, &quot;&quot;, symbol, fixed=TRUE))))
  opClRet &lt;- (Cl(mktdata)/Op(mktdata))-1
  consecutiveDir &lt;- consecutiveTruesExtractor(as.matrix(opClRet&gt;0))
  completeData&lt;- cbind(opClRet,consecutiveDir)
  colnames(completeData) &lt;- c(&quot;OpClRet&quot;,&quot;ConsecutiveDir&quot;)
  
  mktdata$Consecutive &lt;- completeData$ConsecutiveDir
  
  mktdata
}

Symbols &lt;- c(&quot;AMD&quot;,&quot;A&quot;,&quot;AAL&quot;,&quot;ABBV&quot;,&quot;ABT&quot;,&quot;ACAD&quot;,&quot;ADBE&quot;)
             
L &lt;- Map(f, Symbols) 


last_row_values&lt;-lapply(L,tail,1)

At the end of the code, I have created last_row_values xts object to get only the last line of list L.

Now, I'd like to get the stock names filtered by a value of the column "Consecutive".

For example, Which tickers have a value of "2" in the "Consecutive" column of last_row_values?

The desired result is to get names of the tickers with an specific value in the column "Consecutive"

答案1

得分: 1

你可以使用 purrr::map_lgl 来创建一个带有条件结果的向量,然后获取条件通过的向量的名称:

purrr::map_lgl(last_row_values, ~.x$Consecutive == 2) %>% {names(.)[.]}

注:你需要使用 magrittr 管道 %>% 来实现这一点。

英文:

You can use purrr::map_lgl to create a vector with the condition result, then get the names of the vector where the condition passed:

purrr::map_lgl(last_row_values, ~.x$Consecutive == 2) %&gt;% {names(.)[.]}

Obs: you'll need the magrittr pipe %&gt;% for this

huangapple
  • 本文由 发表于 2023年5月17日 20:02:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/76271883.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定