Home > Back-end >  Adding PM and AM to time with binary variable
Adding PM and AM to time with binary variable

Time:07-24

I have a Data frame with two time columns that I need to subtract - var1 and var2 - to create a new variable for the total time between the two variables.

difftime does not give me the correct answer because the times are formated without the AM or PM attached to the value (ex. 11H 30M 0S) - thus it does not know if it subtracting, for example, 11:00pm from 3:00am or 11:00am from 3:00am.

However, I have two subsequent variables, var1a and var2a, with binary values that denote AM or PM (ex. 2 = AM, 1 = PM).

How would I best go about mutating the var1 and var2 variables to add the AM and PM based on the corresponding var1a and var2a binary variables?

Thank you

CodePudding user response:

Here is an approach to do that.

library(tibble)
library(dplyr)
library(stringr)

df <- tibble::tribble(
  ~var1, ~var2, ~var1a, ~var2a,
  "11:00", "3:00", 2, 1,
  "11:00", "3:00", 1, 2
)

am_pm <- function(num){
  dplyr::case_when(
    num == 1 ~ "am",
    num == 2 ~ "pm")
}

df %>% dplyr::mutate(
  time1 =  stringr::str_c(var1, am_pm(var1a)),
  time2 =  stringr::str_c(var2, am_pm(var2a))
)

CodePudding user response:

Using base R you can do something like this:

x <- data.frame(time = c("03:00", "03:00", "11:00", "11:00"),
                am_pm = c(1, 2, 1, 2))

# recode values
x$am_pm[x$am_pm == 1] <- "PM"
x$am_pm[x$am_pm == 2] <- "AM"

# concatenate a new time string
x$time_ampm <- paste(x$time, x$am_pm, sep = " ") |> strptime(format = "%I:%M %p")

str(x)
#> 'data.frame':    4 obs. of  3 variables:
#>  $ time     : chr  "03:00" "03:00" "11:00" "11:00"
#>  $ am_pm    : chr  "PM" "AM" "PM" "AM"
#>  $ time_ampm: POSIXlt, format: "2022-07-24 15:00:00" "2022-07-24 03:00:00" ...

Basically recode your am/pm variables, attach them to your existing string using paste and create POSIXlt objects using strptime with correct format definition. difftime should work on these objects now.

  • Related