Home > Enterprise >  Problems with hms conversions and conditionals
Problems with hms conversions and conditionals

Time:08-31

I have a 2 column df, both characters, but one column is actually a time (h:m) column. I would like to create a new column called "DAY.NIGHT" based on whether the existing time column has a value between 8am-6pm or between 6pm-8am.

I have attempted to use the hms package to convert the existing time column into a hms type and I feel like maybe it has something to do with the other packages I have attached because sometimes my code as it is works, but then randomly it will stop working and I can't get it to work again.

Does anyone have an alternate way perhaps to achieve what I am trying to without using the hms package?

EDIT 1: True to form I just ran it again (without changing anything) and it has randomly worked. Same thing happened yesterday. So again if anyone has an alternate way to achieve this without the hms package it would be great.

EDIT 2 Below is the error I tend to get. The odd thing is it sometimes will work but most of the time I get this error:

<error/dplyr:::mutate_error>
Error in `mutate()`:
! Problem while computing `DAY.NIGHT = ifelse(...)`.
ℹ The error occurred in row 1.
Caused by error:
! All arguments must be numeric or NA
---
Backtrace:
  1. time_agg %>% rowwise() %>% ...
  8. hms::hms(TIME)
  9. hms:::check_args(args)
 10. base::stop("All arguments must be numeric or NA", call. = FALSE)

Here is an extract of my script where the issue is:

library(tidyverse) 
library(ggplot2)
library(lubridate)
library(scales) 
library(viridis) 
library(hrbrthemes) 
library(e1071) 
library(rstatix)
library(GGally)
library(hms)

df_time <- data.frame(
  REGION = rep(c("NSW", "VIC", "QLD", "SA", "TAS"), each=50),
  TIME = rep(c("00:00", "08:00", "12:00", "21:00", "22:00"), each=10))
)

df_time$TIME <- as_hms(df_time$TIME)

day.night <- df_time %>%
  rowwise() %>%
  mutate('DAY.NIGHT'= ifelse(
    hms(TIME) > hms("8:00:00") & 
      hms(TIME) < hms("18:00:00"), "DAY", "NIGHT"))

CodePudding user response:

I get your error with your first line, df_time$TIME <- as_hms(df_time$TIME). I think it's because as_hms expects to have hours, minutes, and seconds, but your input doesn't have seconds. Let's paste on the seconds:

## add :00 seconds for no error
df_time$TIME <- as_hms(paste0(df_time$TIME, ":00"))

Once the TIME column is already hms class, you don't want to use hms() on it again. And we want to be using as_hms() not hms. (hms() looks like it would be appropriate if you were supplying the H, M, and S as separate arguments.)

day.night <- df_time %>%
  mutate('DAY.NIGHT'= ifelse(
    TIME > as_hms("8:00:00") & 
      TIME < as_hms("18:00:00"), "DAY", "NIGHT"))
## no warnings, no errors, should run consistently if your inputs are consistent
day.night
#     REGION     TIME DAY.NIGHT
# 1      NSW 00:00:00     NIGHT
# 2      NSW 00:00:00     NIGHT
# 3      NSW 00:00:00     NIGHT
# 4      NSW 00:00:00     NIGHT
# 5      NSW 00:00:00     NIGHT
# 6      NSW 00:00:00     NIGHT
# ...
  • Related