¿Cuáles son función envía la salida a un nuevo vector?
Pregunta
Supongamos que tengo una llamada hoja.de.datos TLT cuya última línea es la siguiente:
TLT.Open TLT.Close
2010-12-14 92.4 92.14
Y quiero añadir un vector extra llamado TLT.BarColor por lo que se ve así:
TLT.Open TLT.Close TLT.BarColor
2010-12-14 92.4 92.14 "Green"
Esta es una función que "imprime" si era un día barra verde o rojo.
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")}
En lugar de utilizar la función de impresión R () (que sólo se imprime en la consola), lo que la función R usaría para enviar la salida a poblar un nuevo vector?
super_dataframe <- cbind(TLT, apply(TLT, 1, valid_function))
La función de ejemplo no funciona en esta solución. Pero si la función fuera válida, su salida se podría unir de esta manera.
Solución
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"
Otros consejos
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)