Frage

Angenommen, ich habe eine data.frame namens TLT dessen letzte Zeile ist dies:

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

Und ich möchte einen zusätzlichen Vektor hinzuzufügen genannt TLT.BarColor so dass es wie folgt aussieht:

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

Hier ist eine Funktion, dass „druckt“ ob es ein grüner oder roter Balken Tag.

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")}    

Anstelle die print () R-Funktion (die nur druckt Konsole), was R-Funktion würden Sie verwenden, um die Ausgabe zu senden einen neuen Vektor zu füllen?

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

Die Beispielfunktion funktioniert nicht in dieser Lösung. Aber wenn die Funktion gültig ist, es die Ausgabe auf diese Weise befestigt werden kann.

War es hilfreich?

Lösung

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"

Andere Tipps

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)
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top