0

I am trying to break apart a timestamp into the date an hour. The timestamp in the csv looks like: 2018-12-17T12:25:00Z. There are no NAs or irregularities in the timestamps themselves. But when I run the code chunk

 x %>% 
    mutate(
      chicago_mkt_interval = with_tz(gmt_mkt_interval, "America/Chicago"),
      pricedate = as_date(floor_date(chicago_mkt_interval - minutes(1), "day")),
      hour = hour(ceiling_date(chicago_mkt_interval - minutes(1), "hour")),
      hour = ifelse(hour == 0L, 24L, hour),
      minute = minute(chicago_mkt_interval - minutes(1)) + 1
    ) %>% 
    select(-chicago_mkt_interval) %>% 
    select(pricedate, hour, gmt_mkt_interval, everything())

I get an error

Error in mutate_impl(.data, dots) : 
  Evaluation error: missing value where TRUE/FALSE needed.
Calls: %>% ... <Anonymous> -> mutate -> mutate.tbl_df -> mutate_impl -> .Call

And a snippet of the traceback is here:

Error: Column `4` cannot have NA as name 
12.
stop(structure(list(message = "Column `4` cannot have NA as name", 
    call = NULL, cppstack = NULL), class = c("Rcpp::exception", 
"C++Error", "error", "condition"))) 
11.
mutate_impl(.data, dots, caller_env()) 
10.
mutate.tbl_df(., chicago_mkt_interval = with_tz(gmt_mkt_interval, 
    "America/Chicago"), pricedate = as_date(floor_date(chicago_mkt_interval - 
    minutes(1), "day")), hour = hour(ceiling_date(chicago_mkt_interval - 
    minutes(1), "hour")), hour = ifelse(hour == 0L, 24L, hour),  ... 

I'm new-ish to R, so not sure as the best way to fix or improve the error handling.

otterdog2000
  • 343
  • 2
  • 11
  • Please show a small reproducible example with `dput` – akrun Jan 03 '20 at 00:18
  • For error handling, you can either use [tryCatch](https://stackoverflow.com/questions/12193779/how-to-write-trycatch-in-r) from base R or `safely/possibly` from `purrr` – akrun Jan 03 '20 at 00:25
  • R does not have a time object, only a datetime object, so it is best to keep the date and time portions together and only split it when needed. Ronak’s base R answer below is a good example. – Dave2e Jan 03 '20 at 02:12

1 Answers1

1

You can try converting the timestamp to class POSIXct and then extract date and hour from it.

library(dplyr)
library(lubridate)

x %>%
  mutate(chicago_mkt_interval = ymd_hms(gmt_mkt_interval), 
         pricedate = as.Date(chicago_mkt_interval), 
         hour = hour(chicago_mkt_interval))

Or in base R

x$chicago_mkt_interval <- as.POSIXct(x$gmt_mkt_interval, 
                                     format = '%Y-%m-%dT%T', tz = "UTC")
transform(x, pricedate = as.Date(chicago_mkt_interval), 
             hour = as.integer(format(chicago_mkt_interval, "%H")))
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213