I have this type of variable called created.hour in my dataset doc_KM:
POSIXct[1:2560168], format: "2014-09-13 00:00:00"
It is a POSIXct object in the format that we can see above. A little example of this variable is:
[1] "2014-09-13 00:00:00 CEST" "2014-09-12 22:00:00 CEST" [3] "2014-09-11 16:00:00 CEST" "2014-09-13 19:00:00 CEST" [5] "2014-09-14 03:00:00 CEST" "2014-09-10 10:00:00 CEST"
I want to trasform this variable in a cathegorical variable, that analyzes only the hours of this variable, in this type of class: if the hour of the variable is in this range [23:00:00,02:00:00) the new cathegorical variable takes the value equal to 0, if it is in the range [02:00:00,08:00:00), the new cathegorical variable takes the value equal to 1, and so on for this class: [08:00:00,14:00:00),[14:00:00,18:00:00) and [18:00:00,23:00:00). I tried to use the command ifelse but as the the new variable assumes always values equal to 1. I try this command:
doc_KM[, date_KM]<-ifelse(doc_KM$created.hour >= as.POSIXct("02:00:00", format = "%H:%M:%S") &
doc_KM$created.hour < as.POSIXct("08:00:00", format = "%H:%M:%S")),1,(
ifelse(doc_KM$created.hour >= as.POSIXct("08:00:00", format = "%H:%M:%S") &
doc_KM$created.hour < as.POSIXct("14:00:00", format = "%H:%M:%S")), 2,
ifelse(doc_KM$created.hour >= as.POSIXct("14:00:00", format = "%H:%M:%S") &
doc_KM$created.hour < as.POSIXct("18:00:00", format = "%H:%M:%S")), 3,
ifelse(doc_KM$created.hour >= as.POSIXct("18:00:00", format = "%H:%M:%S") &
doc_KM$created.hour < as.POSIXct("23:00:00", format = "%H:%M:%S")), 4), 0)
Aucun commentaire:
Enregistrer un commentaire