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.
Name of output column xts vs. dataframe using ifelse statement
4 messages · Kenneth Spriggs, Joshua Ulrich, ksspriggs at gmail.com +1 more
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.
An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-sig-finance/attachments/20090629/d24ba074/attachment.pl>
Hi Ken, Try colnames. names isn't the right function. HTH Jeff
On Mon, Jun 29, 2009 at 2:18 PM, <ksspriggs at gmail.com> wrote:
Oh, I see, good point. (But should that the column output really have a
name at all?)
On a similar point I can't get names() to work with xts. Here's an example:
TimeIndex <- Sys.time() + seq(1,5,1); x1 <- rnorm(5); x2 <- rnorm(5)
MyData <- data.frame(TimeIndex, x1, x2)
xtsMyData <- xts(MyData[,-1], MyData[,1]); xtsMyData
names(xtsMyData) <- c("Xone", "Xtwo")
#names(xtsMyData) <- c("Timestamp", "Xone", "Xtwo")
head(xtsMyData,3)
x1 x2
2009-06-29 19:13:23.825 -1.05527495 -0.4014843
2009-06-29 19:13:24.825 -1.82892230 -1.1080668
2009-06-29 19:13:25.825 -0.09286556 1.6264407
names(MyData) <- c("Timestamp", "Xone", "Xtwo")
head(MyData, 3)
Timestamp Xone Xtwo 1 2009-06-29 19:13:23.825 -1.05527495 -0.4014843 2 2009-06-29 19:13:24.825 -1.82892230 -1.1080668 3 2009-06-29 19:13:25.825 -0.09286556 1.6264407 Am I doing something wrong? Thanks On Jun 29, 2009 1:05pm, Joshua Ulrich <josh.m.ulrich at gmail.com> wrote:
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) ans nas is.na(test)
if (any(test[!nas]))
ans[test & !nas] if (any(!test[!nas]))
ans[!test & !nas] ans[nas] ans
}
HTH,
Josh
--
On Sun, Jun 28, 2009 at 2:19 PM, Kenneth Spriggsksspriggs 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 x3 BartSimpson xtsBartSimpson QuestionableHeaderName1 xtsBartSimpson$x2, xtsBartSimpson$x3); QuestionableHeaderName1
QuestionableHeaderName2 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 BartSimpson #xtsBartSimpson QuestionableHeaderName1 BartSimpson$x2, BartSimpson$x3); QuestionableHeaderName1
QuestionableHeaderName2 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
-- Subscriber-posting only.
-- If you want to post, subscribe first.
? ? ? ?[[alternative HTML version deleted]]
_______________________________________________ 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.
Jeffrey Ryan jeffrey.ryan at insightalgo.com ia: insight algorithmics www.insightalgo.com