[R-SIG-Finance] Name of output column xts vs. dataframe using ifelse statement

Joshua Ulrich josh.m.ulrich at gmail.com
Mon Jun 29 20:05:30 CEST 2009


Hi Ken,

I'm not sure there's any way for us to *fix* this, since it is a
result of the ifelse() function.  The source below shows the function
replaces the values of "test" with "yes" or "no" depending on the
result of "test".

Further, in the case that the result contains some values from *both*
columns x2 and x3, it's not clear what the column name should be.

> ifelse
function (test, yes, no) {
    storage.mode(test) <- "logical"
    ans <- test
    nas <- is.na(test)
    if (any(test[!nas]))
        ans[test & !nas] <- rep(yes, length.out = length(ans))[test & !nas]
    if (any(!test[!nas]))
        ans[!test & !nas] <- rep(no, length.out = length(ans))[!test & !nas]
    ans[nas] <- NA
    ans
}
<environment: namespace:base>


HTH,
Josh
--
http://www.fosstrading.com



On Sun, Jun 28, 2009 at 2:19 PM, Kenneth Spriggs<ksspriggs at gmail.com> wrote:
> For xts developers.  (If this has been covered before I apologize.)
> The logic ends up fine just the name of the column doesn't actually go
> with the output...
>
> TimeIndex <- Sys.time() + seq(1,5,1); x1 <- rnorm(5); x2 <- rnorm(5);
> x3 <- rnorm(5)
> BartSimpson <- data.frame(TimeIndex, x1, x2, x3)
> xtsBartSimpson <- xts(BartSimpson[,-1], BartSimpson[,1]); xtsBartSimpson
> QuestionableHeaderName1 <- ifelse(xtsBartSimpson$x1 < 100000,
> xtsBartSimpson$x2, xtsBartSimpson$x3);  QuestionableHeaderName1
> QuestionableHeaderName2 <- ifelse(xtsBartSimpson$x1 == 100000,
> xtsBartSimpson$x2, xtsBartSimpson$x3); QuestionableHeaderName2
>
>                            x1         x2         x3
> 2009-06-28 18:54:02 -0.8688258  1.3517895 -0.8920901
> 2009-06-28 18:54:03 -1.6324798  0.4645305  0.8773917
> 2009-06-28 18:54:04  0.8754520 -1.1538617  1.2920259
> 2009-06-28 18:54:05  0.7083217  0.2606429 -0.4256368
> 2009-06-28 18:54:06 -0.4125602  0.6148270 -0.7822110
>                            x1
> 2009-06-28 18:54:02  1.3517895
> 2009-06-28 18:54:03  0.4645305
> 2009-06-28 18:54:04 -1.1538617
> 2009-06-28 18:54:05  0.2606429
> 2009-06-28 18:54:06  0.6148270
>                            x1
> 2009-06-28 18:54:02 -0.8920901
> 2009-06-28 18:54:03  0.8773917
> 2009-06-28 18:54:04  1.2920259
> 2009-06-28 18:54:05 -0.4256368
> 2009-06-28 18:54:06 -0.7822110
>
> # x1 output should be x3 and the second x1 output should be x2
>
> # Let's try it as a just a data.frame as opposed to xts and see what happens:
> TimeIndex <- x1 <- rnorm(5); x2 <- rnorm(5); x3 <- rnorm(5)
> BartSimpson <- data.frame(x1, x2, x3); BartSimpson
> #xtsBartSimpson <- xts(BartSimpson[,-1], BartSimpson[,1]); xtsBartSimpson
> QuestionableHeaderName1 <- ifelse(BartSimpson$x1 < 100000,
> BartSimpson$x2, BartSimpson$x3);  QuestionableHeaderName1
> QuestionableHeaderName2 <- ifelse(BartSimpson$x1 == 100000,
> BartSimpson$x2, BartSimpson$x3); QuestionableHeaderName2
>
>           x1          x2          x3
> 1  0.09949459 -0.01799246  1.39811684
> 2  0.61831865  0.93604736  0.87330695
> 3  1.26746389  1.13709153 -0.02954153
> 4 -1.32262513 -0.09825617 -0.97914918
> 5 -1.83912138  0.44640603  0.89351793
> [1] -0.01799246  0.93604736  1.13709153 -0.09825617  0.44640603
> [1]  1.39811684  0.87330695 -0.02954153 -0.97914918  0.89351793
>
> # The difference in output between xts and data.frame is that the
> data.frame doesn't have an output column title at all.
>
> _______________________________________________
> R-SIG-Finance at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-sig-finance
> -- Subscriber-posting only.
> -- If you want to post, subscribe first.
>



More information about the R-SIG-Finance mailing list