Reputation: 2493
I am attempting to compare two values in xts
, by subsetting the specific positions of the values. Since I cannot get the result in xts
I have tried to extract the coredata()
into a data frame. The comparison in the data frame fails also.
Question: Why does it fail to perform the comparison in xts
and data frame?
Temp solution: Extract the values into vectors and compare them. It is not a solution since I need to compare many values in a big xts/dataframe.
Wanted solution: I need to be able to compare values, by subsetting in both xts and dataframe. This should be done without loading more packages then having dataframe from R core, and installing xts
.
Below you see the different variations of my attempts:
#########################################
# Create dataframe [df1]
#########################################
date <- as.POSIXct(c("2018-10-01 09:01:00", "2018-10-01 09:02:00"))
open <- c(0, 1)
high <- c(0, 4)
low <- c(0, 3)
close <- c(0, 6)
df1 <- data.frame(
date,
open,
high,
low,
close
)
#############
# Create xts1
#############
# Build an xts based on dataframe components
xts1 <- xts(df1[-1], order.by=df1[,1])
##########################################################
# Attempt 1 to compare xts(column2,row2 with column3,row2)
##########################################################
isTRUE(xts1[2,2] > xts1[2,3]) # Returns false, why?
# Tests:
xts1[2,2] # Not stored, just for printout confirmation.
xts1[2,3] # Not stored, just for printout confirmation.
isTRUE(4 > 3) # Returns true, correct.
####################################
# Attempt 2 - move xts to dataframe.
####################################
df1 <- coredata(xts1)
isTRUE(df1[2,2] > df1[2,3]) # Returns false, why?
# Tests:
df1[2,2] # Not stored, just for printout confirmation.
df1[2,3] # Not stored, just for printout confirmation.
###################################################
# Attempt 3 - move xts to dataframe, extract values
###################################################
df2 <- coredata(xts1)
extracted.value.1 <- as.numeric(df2[2,2]) # Extract value
extracted.value.2 <-as.numeric(df2[2,3]) # Extract value
isTRUE(extracted.value.1 > extracted.value.2) # Returns true, correct.
Here is the info from sessionInfo()
:
> sessionInfo()
R version 3.4.4 (2018-03-15)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 18.04.1 LTS
Matrix products: default
BLAS: /usr/lib/x86_64-linux-gnu/blas/libblas.so.3.7.1
LAPACK: /usr/lib/x86_64-linux-gnu/lapack/liblapack.so.3.7.1
locale:
[1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C LC_TIME=sv_SE.UTF-8 LC_COLLATE=en_US.UTF-8 LC_MONETARY=sv_SE.UTF-8 LC_MESSAGES=en_US.UTF-8
[7] LC_PAPER=sv_SE.UTF-8 LC_NAME=C LC_ADDRESS=C LC_TELEPHONE=C LC_MEASUREMENT=sv_SE.UTF-8 LC_IDENTIFICATION=C
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] xts_0.11-2 zoo_1.8-4
loaded via a namespace (and not attached):
[1] compiler_3.4.4 tools_3.4.4 grid_3.4.4 lattice_0.20-35
Result of dput(xts1)
structure(c(0, 1, 0, 4, 0, 3, 0, 6), .Dim = c(2L, 4L), .Dimnames = list(
NULL, c("open", "high", "low", "close")), index = structure(c(1538377260,
1538377320), tzone = "", tclass = c("POSIXct", "POSIXt")), class = c("xts",
"zoo"), .indexCLASS = c("POSIXct", "POSIXt"), tclass = c("POSIXct",
"POSIXt"), .indexTZ = "", tzone = "")
Result of dput(df1)
structure(c(0, 1, 0, 4, 0, 3, 0, 6), .Dim = c(2L, 4L), .Dimnames = list(
NULL, c("open", "high", "low", "close")))
Upvotes: 3
Views: 142
Reputation: 226522
Got it! It's an R-version problem, not an xts
-version problem.
This is a known "infelicity" in isTRUE()
, described here and fixed after version 3.5.
Note: prior to R 3.5 isTRUE (the current version!) was defined as “isTRUE <- function(x) identical(x, TRUE)” (please see change-log here). This seemed clever, but failed on named logical values (violating a principle of least surprise):
You can update R, or redefine isTRUE
as
isTRUE <- function(x) { is.logical(x) && length(x) == 1 && !is.na(x) && x }
or use isTRUE(unname(x),unname(y))
. Here's what the current version of ?isTRUE
says:
‘isTRUE(x)’ is the same as ‘{ is.logical(x) && length(x) == 1 && !is.na(x) && x }’; ‘isFALSE()’ is defined analogously. Consequently, ‘if(isTRUE(cond))’ may be preferable to ‘if(cond)’ because of ‘NA’s. In earlier R versions, ‘isTRUE <- function(x) identical(x, TRUE)’, had the drawback to be false e.g., for ‘x <- c(val = TRUE)’.
Upvotes: 4