Pergunta

Suppose I have a data.frame named TLT whose last line is this:

           TLT.Open  TLT.Close 
2010-12-14     92.4      92.14   

And I want to add an extra vector called TLT.BarColor so it looks like this:

           TLT.Open  TLT.Close  TLT.BarColor
2010-12-14     92.4      92.14       "Green"

Here is a function that "prints" whether it was a green or red bar day.

bar_color <- function(ticker) {

require("quantmod")

x <- getSymbols(ticker, auto.assign=FALSE)

open        <- x[,1]                       
close       <- x[,2]                       

last_open   <- tail(open,  n=1)            
last_close  <- tail(close, n=1)            



if       (last_open > last_close)    
           {print("Red Bar")} 

else if  (last_open < last_close)          
           {print("Green Bar")}   

 else       {print("Doji Bar")}    

Instead of using the print() R function (which only prints to console), what R function would you use to send the output to populate a new vector?

super_dataframe <- cbind(TLT, apply(TLT, 1, valid_function))

The sample function does not work in this solution. But if the function were valid, it's output could be attached in this manner.

Foi útil?

Solução

ticker can't be a dataframe, but has to be a character. So with the apply you use to create your super data frame, you'll have a problem. THe following function gives the labels for different tickers.

bar_color <- function(ticker){
   x <- getSymbols(ticker,auto.assign=FALSE)
   n <- nrow(x)
   switch(
      sign(x[n,1]-x[n,4])+2,
      "Green Bar",
      "Doji Bar",
      "Red Bar")
}

> TLT <- c("F","QQQQ")
> cbind(TLT,sapply(TLT,bar_color))
     TLT               
F    "F"    "Green Bar"
QQQQ "QQQQ" "Red Bar"  

If you want the labels for one ticker but different dates, then this is what you're looking for :

bar_color <- function(ticker){
   x <- as.data.frame(getSymbols(ticker,auto.assign=FALSE))

   x$barcolor <- sapply(
            as.numeric(sign(x[,1]-x[,4])+2),
            function(j) switch(j,"Green Bar","Doji Bar","Red Bar")
   )

   return(x)
}

> head(bar_color("F"))
           F.Open F.High F.Low F.Close F.Volume F.Adjusted  barcolor
2007-01-03   7.56   7.67  7.44    7.51 78652200       7.51   Red Bar
2007-01-04   7.56   7.72  7.43    7.70 63454900       7.70 Green Bar
2007-01-05   7.72   7.75  7.57    7.62 40562100       7.62   Red Bar
2007-01-08   7.63   7.75  7.62    7.73 48938500       7.73 Green Bar
2007-01-09   7.75   7.86  7.73    7.79 56732200       7.79 Green Bar
2007-01-10   7.79   7.79  7.67    7.73 42397100       7.73   Red Bar

The problem you -likely- face is the fact that getSymbols does not return you a dataframe, but an xts object. For xts there are specific methods to access and add data, and one should not expect this to behave like a data frame.

> X <- getSymbols("F",auto.assign=FALSE)
> class(X)
[1] "xts" "zoo"

Outras dicas

If you changed the print statements to simply the character vector itself, e.g."Red Bar", you can add that to an existing vector such as the last row. It might be clearer code if you substituted return() for the print()'s. The only problem is that a vector needs to be of all the same mode so you would need to accept a character vector or use a one line data.frame.

vec <- c(TLT[NROW(TLT), ] , bar.color( "TLT") )  # a character vector

onerowdf <- cbind( TLT[NROW(TLT), ], bar.color( "TLT")) ) 
# a data.frame (aka list)
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top