-2

我想知道为什么这个 R 语句无法解释我的数据集列中提供的时间格式Occurred Date / Time?我正在尝试按发生日期/时间列索引 .dataframe 和顺序。

> head (test[,c(0:4)])
# A tibble: 6 x 4
  `Template ID` `Reported Date` `Reported Time` `Occurred Date / Time`
          <int>           <chr>          <time>                 <dttm>
1        124529       1/02/2016        00:32:00    2016-01-31 23:19:20
2        126305       6/02/2016        06:42:00    2016-02-06 05:46:48
3        126921       6/02/2016        12:28:00    2016-02-06 10:22:05
4        127035       9/02/2016        04:26:00    2016-02-09 03:59:58
5        127916      11/02/2016        17:25:00    2016-02-11 16:35:19
6        128340      10/02/2016        10:38:00    2016-02-10 09:33:01

xts(test[, -1], order.by = as.POSIXct(test[,c(4)],format="%d-%m-%Y  %H:%M:%S")) 

Error message;
 > xts(test[, -1], order.by = as.POSIXct(test[,c(4)],format="%d-%m-%Y  %H:%M:%S"))
Error in as.POSIXct.default(test[, c(4)], format = "%d-%m-%Y  %H:%M:%S") : 
  do not know how to convert 'test[, c(4)]' to class “POSIXct”
4

1 回答 1

1

检查您的列Ocurred Date/Time(示例2016-02-10 09:33:01),格式为 "%Y-%m-%d %H:%M:%S" 但您设置了format="%d-%m-%Y %H:%M:%S"

如果您在格式方面遇到问题,图书馆anytime可能会帮助您:

library(anytime)
anytime("2016-02-10 09:33:01") 

它试图猜测你日期的格式!

于 2019-03-10T10:37:35.883 回答