我有3个xts对象
logged <- xts::xts(x = loggedInUsers$loggedInUsers, order.by = Sys.time())
loadValue <- xts::xts(x = loadAvg, order.by = Sys.time())
hostname <- xts::xts(x = loadHost, order.by = Sys.time())
dput(hostname)
dput(loadValue)
dput(logged)
dput给出以下结果
structure("deliverforgoodportal", .Dim = c(1L, 1L), index = structure(1551088127.27724, tzone = "", tclass = c("POSIXct",
"POSIXt")), class = c("xts", "zoo"), .indexCLASS = c("POSIXct",
"POSIXt"), tclass = c("POSIXct", "POSIXt"), .indexTZ = "", tzone = "")
structure(0, .Dim = c(1L, 1L), .Dimnames = list(NULL, "load"), index = structure(1551088127.27676, tzone = "", tclass = c("POSIXct",
"POSIXt")), .indexCLASS = c("POSIXct", "POSIXt"), tclass = c("POSIXct",
"POSIXt"), .indexTZ = "", tzone = "", class = c("xts", "zoo"))
structure(1, .Dim = c(1L, 1L), index = structure(1551088127.27637, tzone = "", tclass = c("POSIXct",
"POSIXt")), class = c("xts", "zoo"), .indexCLASS = c("POSIXct",
"POSIXt"), tclass = c("POSIXct", "POSIXt"), .indexTZ = "", tzone = "")
当我合并这三个并打印时,主机名将转换为NA
tmp <- merge.xts(hostname, logged, loadValue, all = TRUE)
print(tmp)
输出为:(主机名为NA)
hostname logged load
2019-02-25 09:48:47 NA 1 NA
2019-02-25 09:48:47 NA NA 0
2019-02-25 09:48:47 NA NA NA
为什么这会成为NA?
您应该意识到xts对象是时间序列和矩阵。现在矩阵只能包含一种类型的值,无论是字符还是数字。但不是两个。您的合并尝试将字符值矩阵(主机名)与数值(已记录和加载)组合在一起。这导致主机名值被强制转换为NA。
如果要加入此数据,则必须使用data.frame(或data.table)。另请注意,您的时间值不相等,它们是毫秒。因此,如果你想在几分钟内加入,首先使用来自lubridate包的floor_date
。见下面两个有和没有lubridate的例子。我使用包timetk将xts对象转换为tibble,但取决于您可能不需要的源数据。
with full_join,no lubridate
library(timetk)
library(dplyr)
hostname <- tk_tbl(hostname)
loadValue <- tk_tbl(loadValue)
logged <- tk_tbl(logged)
hostname %>%
full_join(loadValue) %>%
full_join(logged,
by = "index",
suffix = c("_hostname", "_logged"))
Joining, by = "index"
# A tibble: 3 x 4
index value_hostname load value_logged
<dttm> <chr> <dbl> <dbl>
1 2019-02-25 10:48:47 deliverforgoodportal NA NA
2 2019-02-25 10:48:47 NA 0 NA
3 2019-02-25 10:48:47 NA NA 1
与lubridate和左连接:
hostname %>%
mutate(index = lubridate::floor_date(index, unit = "seconds")) %>%
left_join(loadValue %>% mutate(index = lubridate::floor_date(index, unit = "seconds"))) %>%
left_join(logged %>% mutate(index = lubridate::floor_date(index, unit = "seconds")),
by = "index",
suffix = c("_hostname", "_logged"))
Joining, by = "index"
# A tibble: 1 x 4
index value_hostname load value_logged
<dttm> <chr> <dbl> <dbl>
1 2019-02-25 10:48:47 deliverforgoodportal 0 1