Home > Software design >  apple health time series as.xts Error in as.POSIXlt.character(x, tz, ...) : character string is not
apple health time series as.xts Error in as.POSIXlt.character(x, tz, ...) : character string is not

Time:10-27

I am trying to convert times series data from Apple Health export into an xts format. However, I can not manage to get the right data format.

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

Error in as.POSIXlt.character(x, tz, ...) : character string is not in a standard unambiguous format

I checked the class an everything seems fine though... I also tried to specify the format

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

but this didn't work either.

Maybe I am on the wrong track, I appreciate your help! Best C

CodePudding user response:

As @phiver mentioned you need to keep only numeric data in the xts matrix. Also since the Date column is already of type POSIXct you don't need to change that. Try -

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