Rose
Rose

Reputation: 1

Categorizing Data in R between two time periods

I am trying to create a new column in my dataframe and have it show either "DENTRO HORÁRIO" or "FUERA HORÁRIO" when it is between a certain time or if it is outside that time. I thought the best way to do that would be an ifelse statement but I keep getting errors, what is the best way to do this?

Start1 <- format('7:00:00', format = '%H:%M:%S')
End1 <- format('19:00:00', format = "%H:%M:%S")
Ready_For_Audit$Alistamiento.horario.habil.o.fuera.horario <- Ready_For_Audit %>% 
ifelse(Start1<=format(Hora.solicitud, format = '%H:%M:%S')<=End1,
"DENTRO HORÁRIO", "FUERA HORÁRIO")

Upvotes: 0

Views: 236

Answers (1)

Ray
Ray

Reputation: 2278

you have been close. In R you cannot combine logical conditions in form of a two-sided equation of the form lower-limit <= variable <= upper-limit. For this you have to combine both parts of the equation, i.e. lower-limit <= variable & variable <= upper-limit. Note: if you need more conditions just construct them through a combination of logical ANDs and ORs, etc.

You did not provide a reproducible example. Thus, I create a timeline for a day. You may have to adapt this for your case. I use the sequence function seq() which allows me to set the steps. For demo purposes I chose 2 hours. You can pick any step that is useful for you, e.g. 30 mins.

For handling dates and times I use the {lubridate} package, With hour() you can extract the hour from a timestamp. I compare this against your opening/closing hours.

Please bear with me, I changed/truncated the variable names ;)

# create timeline
library(lubridate)  # for datetime handling
library(dplyr)

df <- tibble(
    datetime = seq(from = ymd_hm("2021-05-11 00:00")
                   ,to  = ymd_hm("2021-05-11 24:00")
                   , by = "2 hours")
)

# apply ifelse to timeline
df %>% 
## ---------------  check whether hour is larger/equal 7 and smaller than/equal 17
##                  adapt as appropriate or use your Start1, End1 variable.
   mutate(Alistamiento = ifelse(hour(datetime) >= 7 & hour(datetime) <= 17
                                , "DENTRO HORÁRIO", "FUERA HORÁRIO" )
         )

This yields:

# A tibble: 13 x 2
   datetime            Alistamiento  
   <dttm>              <chr>         
 1 2021-05-11 00:00:00 FUERA HORÁRIO 
 2 2021-05-11 02:00:00 FUERA HORÁRIO 
 3 2021-05-11 04:00:00 FUERA HORÁRIO 
 4 2021-05-11 06:00:00 FUERA HORÁRIO 
 5 2021-05-11 08:00:00 DENTRO HORÁRIO
 6 2021-05-11 10:00:00 DENTRO HORÁRIO
 7 2021-05-11 12:00:00 DENTRO HORÁRIO
 8 2021-05-11 14:00:00 DENTRO HORÁRIO
 9 2021-05-11 16:00:00 DENTRO HORÁRIO
10 2021-05-11 18:00:00 FUERA HORÁRIO 
11 2021-05-11 20:00:00 FUERA HORÁRIO 
12 2021-05-11 22:00:00 FUERA HORÁRIO 
13 2021-05-12 00:00:00 FUERA HORÁRIO 

Upvotes: 1

Related Questions