Я пытаюсь сравнить два значения в xts
путем подстановки определенных позиций значений.Поскольку я не могу получить результат в xts
, я попытался извлечь coredata()
в кадр данных.Сбой сравнения во фрейме данных также.
Вопрос: Почему не удается выполнить сравнение в xts
и фрейме данных?
Temp решение: Извлеките значения в векторы и сравните их.Это не решение, так как мне нужно сравнить много значений в большом xts / dataframe.
Требуемое решение: Мне нужно иметь возможность сравнивать значения путем поднабора в xts и dataframe.Это должно быть сделано без загрузки большего количества пакетов, чем с кадром данных из ядра 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")))