apple health time series as.xts Error in as.POSIXlt.character(x, tz, ...) : 字符串不是标准的明确格式

apple health time series as.xts Error in as.POSIXlt.character(x, tz, ...) : character string is not in a standard unambiguous format

我正在尝试将 Apple Health 导出的时间序列数据转换为 xts 格式。 但是,我无法获得正确的数据格式。

new = health_df %>% 
  mutate(
    Type = str_remove(type, "HKQuantityTypeIdentifier"), 
    Value = as.numeric(as.character(value)),
    Date = as_datetime(creationDate)) %>%
  filter(Type == 'HeartRate') %>% 
  select(Date, Type, Value)
> head(new)
# A tibble: 6 x 3
  Date                Type      Value
  <dttm>              <chr>     <dbl>
1 2021-07-27 13:12:32 HeartRate    80
2 2021-07-27 13:12:38 HeartRate    79
3 2021-07-27 13:12:42 HeartRate    76
4 2021-07-27 13:12:47 HeartRate    73
5 2021-07-27 13:12:52 HeartRate    71
6 2021-07-27 13:12:57 HeartRate    71
> class(new$Date)
[1] "POSIXct" "POSIXt" 
> new = as.xts(new, dateFormat = "POSIXct")
Error in as.POSIXlt.character(x, tz, ...) : 
  character string is not in a standard unambiguous format

as.POSIXlt.character(x, tz, ...) 错误: 字符串不是标准的明确格式

我检查了 class,虽然一切似乎都很好...... 我也试过指定格式

Date = as.POSIXct((Date), format = "%Y.%m.%d %H:%M:%S", tz="GMT"))

但这也不起作用。

可能是我走错了路,感谢您的帮助! 最好 C

正如@phiver 提到的,您只需要在 xts 矩阵中保留数字数据。此外,由于 Date 列已经是 POSIXct 类型,因此您无需更改它。尝试-

new_xts <- xts::xts(new$Value, order.by = new$Date)