Search code examples
rchron

looping in order to create dummy variables R


I want to make a dataframe with a dummy variable that shows 1 when the time of incident is between a range of time (hours). That is what I made:

library(chron)
library(stringr)

var <- c("A", "B", "C", "D", "E", "F")
times <- c("22:40:20", "10:36:29", "09:23:27", "12:33:27", "12:22:22", "00:17:44")

df <- data.frame(var, times)
hours <- print(paste(stringr::str_pad(c(0:23), 2, side="left", pad=0),
                     "00", "00", sep=":"))

hours <- chron(times=hours)
df$times <- chron(times=df$times)

df$zero <- 0
df$zero[df$times>=hours[1] & df$times<hours[2]] <- 1
df$one <- 0
df$one[(df$times>=hours[2] & df$times<hours[3])] <- 1

and so on and soon until:

df$twentytree <- 0
df$twentytree[(df$times>=hours[24] ] <- 1

  var    times zero one
1   A 22:40:20    0   0
2   B 10:36:29    0   0
3   C 09:23:27    1   0 # in column zero is one because the time event is between 00:00:00 and 01:00:00
4   D 12:33:27    0   0
5   E 12:22:22    0   0
6   F 00:17:44    1   0

but I'm pretty sure that must be some other automatized way Ideas?

Thx in advanced.

p.d: I've tried this way but i can't achieve it.

df <- cbind(df, sapply(hours, function(x) as.integer(c4$times >=x & c4$times<x) ) )

Solution

  • At the end i not even need to convert to dates. I get it this way:

    library(stringr)
    
    var <- c("A", "B", "C", "D", "E", "F")
    times <- c("22:40:20", "10:36:29", "09:23:27", "12:33:27", "12:22:22", "00:17:44")
    
    df <- data.frame(var, times)
    
    hours <- stringr::str_pad(c(0:23), 2, side="left", pad=0)
    df$hour <- substr(df$times,1,2)
    
    df <- cbind(df, sapply(hours, function(x) as.integer(df$hour==x) ) )
    

    and the result:

     var    times hour 00 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21
    1   A 22:40:20   22  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0
    2   B 10:36:29   10  0  0  0  0  0  0  0  0  0  0  1  0  0  0  0  0  0  0  0  0  0  0
    3   C 09:23:27   09  0  0  0  0  0  0  0  0  0  1  0  0  0  0  0  0  0  0  0  0  0  0
    4   D 12:33:27   12  0  0  0  0  0  0  0  0  0  0  0  0  1  0  0  0  0  0  0  0  0  0
    5   E 12:22:22   12  0  0  0  0  0  0  0  0  0  0  0  0  1  0  0  0  0  0  0  0  0  0
    6   F 00:17:44   00  1  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0