otterdog2000
otterdog2000

Reputation: 363

How do I properly use ifelse to handle a timestamp mutate in R?

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.

Upvotes: 0

Views: 260

Answers (1)

Ronak Shah
Ronak Shah

Reputation: 389135

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")))

Upvotes: 1

Related Questions