使用 emphasize.strong.cells 强调单元格失败 - POSIXct 相关?

Emphasizing cells using emphasize.strong.cells fails - POSIXct related?

我正在尝试显示以下 table(其中包含两列 class POSIXct)并强调包含特定值 ("B") 的单元格。

> test
   bikeid start_station           starttime end_station             endtime
1       1             A 2017-09-25 01:00:00           B 2017-09-25 01:30:00
2       1             B 2017-09-25 07:30:00           C 2017-09-25 08:00:00
3       1             C 2017-09-25 10:00:00           A 2017-09-25 10:30:00
4       1             A 2017-09-25 13:00:00           C 2017-09-25 13:30:00
5       1             C 2017-09-25 15:30:00           B 2017-09-25 16:00:00
6       1             B 2017-09-25 18:00:00           B 2017-09-25 18:30:00
7       1             B 2017-09-25 19:00:00           A 2017-09-25 19:30:00
8       1             А 2017-09-25 20:00:00           C 2017-09-25 20:30:00
9       1             C 2017-09-25 22:00:00           B 2017-09-25 22:30:00
10      1             B 2017-09-25 23:00:00           C 2017-09-25 23:30:00

当我使用下面的代码时,成功显示了table:

library(pander)
panderOptions('table.split.table', Inf)
pander(test)

但是,当我包含 emphasize.strong.cells 来指定要强调的单元格时, table 失败,我收到错误消息:

panderOptions('table.split.table', Inf)
emphasize.strong.cells(which(test == "B"))
pander(test)

# Error in as.POSIXlt.character(x, tz, ...) : 
#  character string is not in a standard unambiguous format
#  Calls: <Anonymous> ... as.POSIXct.default -> as.POSIXct -> as.POSIXlt -> 
# as.POSIXlt.character
# Execution halted

即使包含 POSIXct 值的列被完全删除,代码仍然失败。

为什么会发生这种情况,正确的解决方法是什么?

这是数据集的示例:

> dput(test)
structure(list(bikeid = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1), start_station = c("A", 
"B", "C", "A", "C", "B", "B", "А", "C", "B"), starttime = structure(c(1506315600, 
1506339000, 1506348000, 1506358800, 1506367800, 1506376800, 1506380400, 
1506384000, 1506391200, 1506394800), class = c("POSIXct", "POSIXt"
), tzone = ""), end_station = c("B", "C", "A", "C", "B", "B", 
"A", "C", "B", "C"), endtime = structure(c(1506317400, 1506340800, 
1506349800, 1506360600, 1506369600, 1506378600, 1506382200, 1506385800, 
1506393000, 1506396600), class = c("POSIXct", "POSIXt"), tzone = "")), .Names = c("bikeid", 
"start_station", "starttime", "end_station", "endtime"), row.names = c(NA, 
-10L), class = "data.frame")

您的问题与emphasize.strong.cells无关,而是由您在which中进行的比较引起的:test == "B"。您正在将 character "B" 与整个数据集 "test" 进行比较。在 "test" 中,你的焦点变量是 character,但你也有一些非 character 变量,即两个 POSIXct 变量。因此,您正在对不同类型的参数进行比较 (==)。然后看?Comparison:

If the two arguments are atomic vectors of different types, one is coerced to the type of the other.

当您尝试将 "B" 与 POSIXct 变量进行比较时,R 会尝试将 "B" 强制转换为 POSIXct,但失败了:

as.POSIXct("2017-09-25 01:00:00") == "B"
# Error in as.POSIXlt.character(x, tz, ...) : 
#   character string is not in a standard unambiguous format

只是为了展示 RHS 的强制转换 character 何时会 起作用的例子:

as.POSIXct("2017-09-25 01:00:00") == "2017-09-25 01:00:00"
# [1] TRUE 

如果您的数据框中有一个 Date 变量,则会发生类似的问题:

as.Date("2017-09-25") == "B"
# Error in charToDate(x) : 
#   character string is not in a standard unambiguous format

因此,正如我在评论中提到的,您的情况的一种解决方法是在进行比较之前将 POSIXct 列强制为 character


另一方面,如果您一直在寻找某个 numeric 值来强调(例如 test > 10),则与 DatePOSIXct 列不会导致错误(检查例如 typeof(as.POSIXct("2017-09-25 01:00:00")))。


一个小例子:

d <- data.frame(time = as.POSIXct("2017-09-25 21:30:20") + 0:1,
                x = c("A", "B"), y = 1:2)
d

emphasize.strong.cells(which(d == "B", arr.ind = TRUE))
# Error in as.POSIXlt.character(x, tz, ...) : 
#   character string is not in a standard unambiguous format

d$time <- as.character(d$time)
emphasize.strong.cells(which(d == "B", arr.ind = TRUE))
pander(d)
# ---------------------------------
#          time             x     y 
# --------------------- ------- ---
#   2017-09-25 21:30:20     A     1 
# 
#   2017-09-25 21:30:21   **B**   2 
# ---------------------------------


emphasize.strong.cells(which(d == 1, arr.ind = TRUE))
pander(d)
# ---------------------------------
#         time           x     y   
# --------------------- --- -------
#  2017-09-25 21:30:20   A   **1** 
#  
#  2017-09-25 21:30:21   B     2   
# ---------------------------------