Home > Back-end >  combining 2 functions into one in R
combining 2 functions into one in R

Time:12-23

ticker<-c("AAPL","TSLA")

quote<-function(ticker){
  quote<-quantmod::getQuote(ticker)$Last
  quote
}

trade_time<-function(ticker){
  quantmod::getQuote(ticker)$'Trade Time'
}

> quote(ticker)
[1] 131.57 128.91
> trade_time(ticker)
[1] "2022-12-22 11:33:56 EST" "2022-12-22 11:33:57 EST"

May I know if there is a way I can combine the 2 functions into one? Thanks a lot.

CodePudding user response:

We could extract both the columns at once by selecting the columns from the data

trade_time <- function(ticker)
{

quantmod::getQuote(ticker)[, c("Trade Time", "Last")]
}
  • Related