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

Kenneth Spriggs ksspriggs at gmail.com
Sun Jun 28 21:19:06 CEST 2009


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.



More information about the R-SIG-Finance mailing list