test bikeid start_station starttim-6ren">
gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 17:49:16 25 4
gpt4 key购买 nike

我正在尝试显示下表(其中包含类 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")

最佳答案

您的问题与emphasize.strong.cells无关, 但是由您在 which 中进行的比较引起的: test == "B" .您正在比较 character “B”与整个数据集“测试”。在“测试”中,您的焦点变量是 character s,但你也有一些非 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在进行比较之前。


另一方面,如果您一直在寻找要强调的某个数字值(例如 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
# ---------------------------------

关于r - 使用 emphasis.strong.cells 强调单元格失败 - POSIXct 相关?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46417229/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com