xts 和数据帧中单个值的比较失败
Comparison of single values in xts and data frame fails
我试图通过对值的特定位置进行子集化来比较 xts
中的两个值。由于我无法在 xts
中获得结果,因此我尝试将 coredata()
提取到数据框中。数据框中的比较也失败。
问题:为什么在xts
和data frame中比较失败?
临时解决方案:将值提取到向量中并进行比较。这不是一个解决方案,因为我需要在一个大 xts/dataframe 中比较许多值。
想要的解决方案: 我需要能够通过在 xts 和数据帧中进行子集化来比较值。这应该在不加载更多包然后从 R 核心获取数据框并安装 xts
.
的情况下完成
下面是我尝试的不同变体:
#########################################
# 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.
这是来自 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
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 = "")
dput(df1)的结果
structure(c(0, 1, 0, 4, 0, 3, 0, 6), .Dim = c(2L, 4L), .Dimnames = list(
NULL, c("open", "high", "low", "close")))
知道了!这是一个 R 版本问题,而不是 xts
版本问题。
这是 isTRUE()
中已知的 "infelicity",描述 here 并在 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):
您可以更新 R,或将 isTRUE
重新定义为
isTRUE <- function(x) { is.logical(x) && length(x) == 1 && !is.na(x) && x }
或使用isTRUE(unname(x),unname(y))
。 ?isTRUE
的当前版本是这样说的:
‘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)’.
我试图通过对值的特定位置进行子集化来比较 xts
中的两个值。由于我无法在 xts
中获得结果,因此我尝试将 coredata()
提取到数据框中。数据框中的比较也失败。
问题:为什么在xts
和data frame中比较失败?
临时解决方案:将值提取到向量中并进行比较。这不是一个解决方案,因为我需要在一个大 xts/dataframe 中比较许多值。
想要的解决方案: 我需要能够通过在 xts 和数据帧中进行子集化来比较值。这应该在不加载更多包然后从 R 核心获取数据框并安装 xts
.
下面是我尝试的不同变体:
#########################################
# 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.
这是来自 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
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 = "")
dput(df1)的结果
structure(c(0, 1, 0, 4, 0, 3, 0, 6), .Dim = c(2L, 4L), .Dimnames = list(
NULL, c("open", "high", "low", "close")))
知道了!这是一个 R 版本问题,而不是 xts
版本问题。
这是 isTRUE()
中已知的 "infelicity",描述 here 并在 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):
您可以更新 R,或将 isTRUE
重新定义为
isTRUE <- function(x) { is.logical(x) && length(x) == 1 && !is.na(x) && x }
或使用isTRUE(unname(x),unname(y))
。 ?isTRUE
的当前版本是这样说的:
‘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)’.