我正在尝试显示下表(其中包含两列类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
当我使用以下代码时,表格已成功显示:
library(pander)
panderOptions('table.split.table', Inf)
pander(test)
但是,当我包含emphasize.strong.cells
来指定要强调的细胞时,
表失败,我收到一个错误:
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")
答案 0 :(得分:1)
您的问题与emphasize.strong.cells
无关,但这是由您在which
:test == "B"
中进行的比较造成的。您正在比较character
&#34; B&#34;使用整个数据集&#34; test&#34;。在&#34; test&#34;中,您的焦点变量为character
s,但您还有一些非character
变量,即两个POSIXct
变量。因此,您在不同类型的参数之间进行比较(==
)。然后查看?Comparison
:
如果两个参数是不同类型的原子矢量,则一个被强制转换为另一个的类型。
当你尝试比较&#34; B&#34;使用POSIXct
变量,R试图强迫&#34; B&#34;到失败的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
。
另一方面,如果您一直在寻找某个数字值来强调(例如test > 10
),则与Date
或POSIXct
进行比较列不会导致错误(请检查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
# ---------------------------------